0

我试图在用户可以预约训练时间时创建应用程序。 当我尝试创建的书我收到了以下方法:控制器中缺少参数#create

ActionController::ParameterMissing in BookingsController#create

param is missing or the value is empty: booking

这是我的预约控制器:

class BookingsController < ApplicationController 
    before_action :load_training, only: [:create] 

    def new 
    @booking = Booking.new 
    end 

    def create 
    @booking = @training.bookings.build(booking_params) 
    @booking.user = current_user 
    if @booking.save 
     flash[:success] = "Book created" 
     redirect_to training_index_url 
    else 
     render 'new' 
    end 
    end 


    def index 
    @booking = Booking.all 
    end 


    def destroy 
    @booking = Booking.find(params[:id]) 
    @booking.destroy 
    flash[:success] = "Book deleted" 
    redirect_to training_index_url 
    end 



private 

    def booking_params 
    params.require(:booking).permit(:user_id, :training_id) 
    end 

    def load_training 
    @training = Training.find_by(params[:training_id]) 
    end 
end 

我不知道为什么犯规采取它的USER_ID参数到预订,我注意到因为我确实把@user放在create方法中,结果是零。当我与用户做到了,我得到了肯定的回答(ID,小时,槽等)

这是我的预订模式:

class Booking < ApplicationRecord 
    belongs_to :user 
    belongs_to :training 
    default_scope -> { order(created_at: :desc) } 
    validates :user_id, presence: true 
    validates :training_id, presence: true 



end 

我的路线RB:

Rails.application.routes.draw do 

    root 'static_pages#home' 
    get '/signup',    to: 'users#new' 
    get '/contact',    to: 'static_pages#contact' 
    get '/about',    to: 'static_pages#about' 
    get '/login',    to: 'sessions#new' 
    post '/login',    to: 'sessions#create' 
    delete '/logout',    to: 'sessions#destroy' 
    get '/book',     to: 'bookings#new' 
    post '/book',     to: 'bookings#create' 
    delete '/unbook',    to: 'bookings#destroy' 


    resources :account_activations, only: [:edit] 
    resources :password_resets,  only: [:new, :create, :edit, :update] 
    resources :bookings,   only: [:create, :destroy] 
    resources :training 
    resources :users 
end 

新预订视图:

<h1>Booking confirmation</h1> 

<%= form_for (@booking) do |f| %> 
    <%= f.submit "Book", class: "btn btn-primary" %> 
<% end %> 

我想现在为什么预订方法不检索user_id参数。

这是我的帖子中看到:

Started POST "/bookings" for 127.0.0.1 at 2017-03-09 00:31:53 -0400 
Processing by BookingsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"EUoo8M66O2XlRgeMoEsSqwH2i/EKKFzMMRsY9UilzcU+T+5n6/Gjx0abxlZ7nJlqgr5wsALtXW/UMf2Q01pNUg==", "commit"=>"Reservar"} 
    Training Load (0.1ms) SELECT "trainings".* FROM "trainings" LIMIT ? [["LIMIT", 1]] 
Completed 400 Bad Request in 2ms (ActiveRecord: 0.1ms) 



ActionController::ParameterMissing (param is missing or the value is empty: booking): 

app/controllers/bookings_controller.rb:36:in `booking_params' 
app/controllers/bookings_controller.rb:9:in `create' 
    Rendering /home/cesar/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb wi 

感谢

+1

你能后的终端在开机时你看到正在发送的。 –

+0

嗨,欢迎来到Stack Overflow ...所以在你的表单/视图中,我看不到'user_id'或'training_id'的字段。你是否忘记包含它们? –

+0

我应该如何添加training_id? – Cesar

回答

0

问题与你的代码是你是不是从您的预订视图发送user_idtraining_id

您应该有user_id这可以是一个隐藏字段和training_id为用户选择的培训。

<%= f.hidden_field :user_id, value: current_user.id %> 

如果用户可以一次预订多个培训,那么您可以拥有带多选选项的培训列表。

它的用户只能在一次交易中预订一次培训,对每次培训的复选框都应该这样做。

<ul> 
    <% @trainings.each do |training| %> 
     <li> 
     <%= check_box_tag 'training_ids[]', training.id -%> 
     <%= h training.name -%> 
     </li> 
    <% end %> 
</ul> 

当用户提交此,您将得到user_id以及training_idstraining_ids = ["1", "2", "3"]

+0

好吧,我有点困惑,即时通讯要让用户每天只能有一个预订,我要在哪里放置迭代@training对象的代码?培训指数? – Cesar

+0

你应该把这段代码放在预订视图中。 – Pramod

+0

它给了我错误:未定义的方法'each'为零:NilClass – Cesar