我試圖在用戶可以預約訓練時間時創建應用程序。 當我嘗試創建的書我收到了以下方法:控制器中缺少參數#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
感謝
你能後的終端在開機時你看到正在發送的。 –
嗨,歡迎來到Stack Overflow ...所以在你的表單/視圖中,我看不到'user_id'或'training_id'的字段。你是否忘記包含它們? –
我應該如何添加training_id? – Cesar