我试着做一个应用,当一个用户可以预订一个培训小时。当我试图创建这本书时,我会收到以下方法:
操作控制器::BookingsController#create中的参数错误 param丢失或值为空:预订
这位是我的订票控制器:
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在创建方法,其结果是零。当我和用户一起做这件事时,我得到了一个肯定的答案(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谢谢
发布于 2017-03-09 04:28:09
代码的问题是您没有从预订视图发送user_id和training_id。
您应该拥有user_id (可以是一个隐藏字段)和training_id (用于用户选择的培训)。
<%= f.hidden_field :user_id, value: current_user.id %>如果用户一次可以预订多个培训,则可以使用多选择选项列出培训列表。
It用户只能在一个事务中预订一次培训,针对每一次培训的复选框应该可以做到这一点。
<ul>
<% @trainings.each do |training| %>
<li>
<%= check_box_tag 'training_ids[]', training.id -%>
<%= h training.name -%>
</li>
<% end %>
</ul>当用户提交此文件时,您将得到user_id和training_ids以及training_ids = ["1", "2", "3"]。
https://stackoverflow.com/questions/42686133
复制相似问题