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