我试图在用户预订培训时间时制作应用。 当我尝试创建这本书时,我收到以下错误:
BookingsController #create中的ActiveRecord :: RecordNotFound
找不到'id'=
的培训
这是我的预订控制器:
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_by(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(params[:training_id])
end
end
我不知道为什么当我尝试写一本新书时,它没有采用training_id参数
当我把@training和@user放在create方法中时,它返回nil。我想知道为什么?我的代码在哪里失败,因此预订模型不会检索user_id和training_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 "Confirm book", class: "btn btn-primary" %>
<% end %>
当我去cmd的培训节目时,它显示了这个:
Started GET "/training/8" for 127.0.0.1 at 2017-03-09 15:39:42 -0400
Processing by TrainingController#show as HTML
Parameters: {"id"=>"8"}
Training Load (0.1ms) SELECT "trainings".* FROM "trainings" WHERE "trainings"."id" = ? LIMIT ? [["id", 8], ["LIMIT", 1]]
Rendering training/show.html.erb within layouts/application
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
DEPRECATION WARNING: Passing an argument to force an association to reload is now deprecated and will be removed in Rails 5.1. Please call `reload` on the result collection proxy instead. (called from _app_views_bookings__booking_form_html_erb__3707831747335467387_70006265917040 at /home/cesar/Apps/boxApp/app/views/bookings/_booking_form.html.erb:4)
Booking Load (0.1ms) SELECT "bookings".* FROM "bookings" WHERE "bookings"."user_id" = ? ORDER BY "bookings"."created_at" DESC [["user_id", 2]]
Rendered bookings/_booking_form.html.erb (5.9ms)
Rendered bookings/_show_bookings.html.erb (0.3ms)
Rendered training/show.html.erb within layouts/application (8.4ms)
Rendered layouts/_header.html.erb (0.6ms)
Rendered layouts/_footer.html.erb (0.2ms)
Completed 200 OK in 43ms (Views: 34.6ms | ActiveRecord: 0.8ms)
它看起来好像有user_id但不是training_id(当我做@user它返回nil tho时)
这是booking_form:
<% unless current_user?(@user) %>
<% if current_user.bookings(@training) %>
<%= link_to "Book", book_path, class: "btn btn-primary" %>
<% else %>
<%= link_to "Unbook", unbook_path, method: "delete", class: "btn btn-primary" %>
<% end %>
<% end %>
点击booking_form中的“预订”时,命令显示:
Started GET "/book" for 127.0.0.1 at 2017-03-09 15:46:23 -0400
Processing by BookingsController#new as HTML
Rendering bookings/new.html.erb within layouts/application
Rendered bookings/new.html.erb within layouts/application (1.8ms)
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
Rendered layouts/_header.html.erb (1.8ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 39ms (Views: 37.2ms | ActiveRecord: 0.1ms)
当我尝试确认书时,命令显示:
Started POST "/bookings" for 127.0.0.1 at 2017-03-09 15:50:25 -0400
Processing by BookingsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"3NrQ7vA2yL5sIlI3Q5nHiywYy4s3QP20amuu4czTCGmKdYy4fTkY4uMpgEyGxAU/kTDPLcfrsRqJZ1YGpEpnKw==", "commit"=>"Reservar"}
Training Load (0.1ms) SELECT "trainings".* FROM "trainings" WHERE "trainings"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]
Completed 404 Not Found in 1ms (ActiveRecord: 0.1ms)
ActiveRecord::RecordNotFound (Couldn't find Training with 'id'=):
由于
答案 0 :(得分:1)
尝试以下代码:
def load_training
@training = Training.find(params[:booking][:training_id])
end
并用以下代码替换load_training函数:
string s = "\\t Hello \\n";
答案 1 :(得分:0)
您发送training_id
作为预订参数的一部分:
params.require(:booking).permit(:user_id, :training_id)
所以你应该这样做:
def load_training
@training = Training.find(params[:booking][:training_id])
end
此外,您需要将其实际添加到表单中,因此应在此点设置@booking.training_id
(在控制器new
操作中)并且您需要将其添加到表单中:
<%= form_for (@booking) do |f| %>
<%= f.hidden_field :training_id %>
<%= f.submit "Confirm book", class: "btn btn-primary" %>
<% end %>