Rails - "Cannot find object with 'id'="
Rails - "Cannot find object with 'id'="
希望有人能帮我解决这个问题。我正在创建一个 nested_form_for Order.new,fields_for 日期Order.new。
在我的orders_controller中,我在'new'和'create'动作中定义了@school,这对order/new和order/show都很重要。然而,Rails 似乎没有找到符合参数的学校。
提交表单时出错:ActiveRecord::RecordNotFound in OrdersController#create
(找不到 'id'= 的学校)
参数:"order"=>{"user_id"=>"2",
"school_id"=>"1", ... } <-- 对我来说很好,:school_id = 1.
Routes.rb
get '/schools/:school_id/orders/new' => 'orders#new'
resources :schools do
resources :orders
end
resources :orders
Orders_controller.rb
def new
@order = Order.new
@order.date_orders.build
@school = School.find(params[:school_id])
end
def create
@school = School.find(params[:school_id])
@order = Order.new(order_params)
if @order.save
flash[:success] = "Thank you for your request. We will get in touch with you shortly."
render @order
else
render 'new'
end
end
def show
@order = Order.find(params[:id])
@user = current_user
if current_user && current_user.school_manager
@school = School.find(SchoolAdmin.find_by(user_id: current_user.id).school_id)
elsif current_user && current_user.district_manager
@district = District.find(DistrictAdmin.find_by(user_id: current_user.id).district_id)
end
end
Orders/new.html.erb
<%= nested_form_for @order do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :school_id, :value => params[:school_id] %>
# SOME MORE FIELDS #
<%= f.fields_for :date_orders do |builder| %>
# SOME FIELDS_FOR #
<% end %>
<% end %>
这是我的日志:
Started POST "/orders" for 174.116.225.174 at 2016-05-06 18:53:52 +0000
Processing by OrdersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"X2mZC7JqEPdygS5vL5Y5ecOEkEdSCoGT6EHhPIIAqqKQlsV4z9xJD4sqhlH3fO5FeO+Ds7T4CWynZCYmhF+ayA==", "order"=>{"user_id"=>"2", "school_id"=>"1", "district_id"=>"1", "facility_school_id"=>"1", "date_orders_attributes"=>{"0"=>{"_destroy"=>"false", "order_date"=>"", "time_start"=>"", "time_end"=>""}}, "purpose"=>"Meeting or workshop", "attendees"=>"Mission High School students", "attendees_number"=>"", "description"=>"", "full_name"=>"", "user_group"=>"Mission High School student", "email"=>"", "phone_number"=>"", "occupation"=>""}, "commit"=>"Request to book"}
School Load (1.4ms) SELECT "schools".* FROM "schools" WHERE "schools"."id" = ? LIMIT 1 [["id", nil]]
Completed 404 Not Found in 3ms (ActiveRecord: 1.4ms)
ActiveRecord::RecordNotFound (Couldn't find School with 'id'=):
app/controllers/orders_controller.rb:13:in `create'
在查询中,您传递了 2 个值:
SELECT "schools".* FROM "schools" WHERE "schools"."id" = ? LIMIT 1 [["id", nil]]
错误是说 school.id = 'id='
.
没有学校
我认为您需要做的是明确地将 @school
关联到 @order
。
在创建函数中,添加:
@school = School.find(params[:school_id])
@order = Order.new(order_params)
@order.school = @school
if @order.save
....
在构造 @order
之前,您可能还需要从 order_params 中删除 school_id。
在 params
散列中,school_id
嵌套在 order
键下,因此您需要使用 params[:order][:school_id]
访问它。现在,params[:school_id]
将 return nil
,这就是为什么您会收到实际上没有 ID 的错误消息。
您的参数:
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"X2mZC7JqEPdygS5vL5Y5ecOEkEdSCoGT6EHhPIIAqqKQlsV4z9xJD4sqhlH3fO5FeO+Ds7T4CWynZCYmhF+ayA==",
"order"=>{
"user_id"=>"2",
"school_id"=>"1",
"district_id"=>"1",
"facility_school_id"=>"1",
"date_orders_attributes"=>{
"0"=>{"_destroy"=>"false", "order_date"=>"", "time_start"=>"", "time_end"=>""}},
"purpose"=>"Meeting or workshop",
"attendees"=>"Mission High School students",
"attendees_number"=>"",
"description"=>"",
"full_name"=>"",
"user_group"=>"Mission High School student",
"email"=>"",
"phone_number"=>"",
"occupation"=>""},
"commit"=>"Request to book"
}
请注意 school_id
嵌套在 order
下。这无法通过 params[:school_id
].
访问
编辑
所以现在从您的评论看来,当通过将 create
更改为 params[:order][:school_id]
来修复时,您会在 new
操作中遇到错误。
往后退一点,只需要在create
动作中改成params[:order][:school_id]
即可。对于 new
操作,您可以将其保留为 params[:school_id]
。
刚刚解决了这个问题。
正如两位好心的评论者所指出的,我从参数中的 :school_id 开始,而我的创建操作在 :order 参数中包含 :school_id 。当我重定向创建 :order 的有缺陷的尝试时,Rails 找不到具有该 ID 的学校。虽然他们的解决方案都不适合我,但他们帮助我找到了解决方案。
我是怎么解决的:
def create
@order = Order.new(order_params)
@school = School.find(@order.school_id)
if @order.save
flash[:success] = "Thank you for your request. We will get in touch with you shortly."
render @order
else
**redirect_to "/schools/#{@school.id}/orders/new"**
end
end
希望有人能帮我解决这个问题。我正在创建一个 nested_form_for Order.new,fields_for 日期Order.new。
在我的orders_controller中,我在'new'和'create'动作中定义了@school,这对order/new和order/show都很重要。然而,Rails 似乎没有找到符合参数的学校。
提交表单时出错:ActiveRecord::RecordNotFound in OrdersController#create
(找不到 'id'= 的学校)
参数:"order"=>{"user_id"=>"2", "school_id"=>"1", ... } <-- 对我来说很好,:school_id = 1.
Routes.rb
get '/schools/:school_id/orders/new' => 'orders#new'
resources :schools do
resources :orders
end
resources :orders
Orders_controller.rb
def new
@order = Order.new
@order.date_orders.build
@school = School.find(params[:school_id])
end
def create
@school = School.find(params[:school_id])
@order = Order.new(order_params)
if @order.save
flash[:success] = "Thank you for your request. We will get in touch with you shortly."
render @order
else
render 'new'
end
end
def show
@order = Order.find(params[:id])
@user = current_user
if current_user && current_user.school_manager
@school = School.find(SchoolAdmin.find_by(user_id: current_user.id).school_id)
elsif current_user && current_user.district_manager
@district = District.find(DistrictAdmin.find_by(user_id: current_user.id).district_id)
end
end
Orders/new.html.erb
<%= nested_form_for @order do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :school_id, :value => params[:school_id] %>
# SOME MORE FIELDS #
<%= f.fields_for :date_orders do |builder| %>
# SOME FIELDS_FOR #
<% end %>
<% end %>
这是我的日志:
Started POST "/orders" for 174.116.225.174 at 2016-05-06 18:53:52 +0000
Processing by OrdersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"X2mZC7JqEPdygS5vL5Y5ecOEkEdSCoGT6EHhPIIAqqKQlsV4z9xJD4sqhlH3fO5FeO+Ds7T4CWynZCYmhF+ayA==", "order"=>{"user_id"=>"2", "school_id"=>"1", "district_id"=>"1", "facility_school_id"=>"1", "date_orders_attributes"=>{"0"=>{"_destroy"=>"false", "order_date"=>"", "time_start"=>"", "time_end"=>""}}, "purpose"=>"Meeting or workshop", "attendees"=>"Mission High School students", "attendees_number"=>"", "description"=>"", "full_name"=>"", "user_group"=>"Mission High School student", "email"=>"", "phone_number"=>"", "occupation"=>""}, "commit"=>"Request to book"}
School Load (1.4ms) SELECT "schools".* FROM "schools" WHERE "schools"."id" = ? LIMIT 1 [["id", nil]]
Completed 404 Not Found in 3ms (ActiveRecord: 1.4ms)
ActiveRecord::RecordNotFound (Couldn't find School with 'id'=):
app/controllers/orders_controller.rb:13:in `create'
在查询中,您传递了 2 个值:
SELECT "schools".* FROM "schools" WHERE "schools"."id" = ? LIMIT 1 [["id", nil]]
错误是说 school.id = 'id='
.
我认为您需要做的是明确地将 @school
关联到 @order
。
在创建函数中,添加:
@school = School.find(params[:school_id])
@order = Order.new(order_params)
@order.school = @school
if @order.save
....
在构造 @order
之前,您可能还需要从 order_params 中删除 school_id。
在 params
散列中,school_id
嵌套在 order
键下,因此您需要使用 params[:order][:school_id]
访问它。现在,params[:school_id]
将 return nil
,这就是为什么您会收到实际上没有 ID 的错误消息。
您的参数:
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"X2mZC7JqEPdygS5vL5Y5ecOEkEdSCoGT6EHhPIIAqqKQlsV4z9xJD4sqhlH3fO5FeO+Ds7T4CWynZCYmhF+ayA==",
"order"=>{
"user_id"=>"2",
"school_id"=>"1",
"district_id"=>"1",
"facility_school_id"=>"1",
"date_orders_attributes"=>{
"0"=>{"_destroy"=>"false", "order_date"=>"", "time_start"=>"", "time_end"=>""}},
"purpose"=>"Meeting or workshop",
"attendees"=>"Mission High School students",
"attendees_number"=>"",
"description"=>"",
"full_name"=>"",
"user_group"=>"Mission High School student",
"email"=>"",
"phone_number"=>"",
"occupation"=>""},
"commit"=>"Request to book"
}
请注意 school_id
嵌套在 order
下。这无法通过 params[:school_id
].
编辑
所以现在从您的评论看来,当通过将 create
更改为 params[:order][:school_id]
来修复时,您会在 new
操作中遇到错误。
往后退一点,只需要在create
动作中改成params[:order][:school_id]
即可。对于 new
操作,您可以将其保留为 params[:school_id]
。
刚刚解决了这个问题。
正如两位好心的评论者所指出的,我从参数中的 :school_id 开始,而我的创建操作在 :order 参数中包含 :school_id 。当我重定向创建 :order 的有缺陷的尝试时,Rails 找不到具有该 ID 的学校。虽然他们的解决方案都不适合我,但他们帮助我找到了解决方案。
我是怎么解决的:
def create
@order = Order.new(order_params)
@school = School.find(@order.school_id)
if @order.save
flash[:success] = "Thank you for your request. We will get in touch with you shortly."
render @order
else
**redirect_to "/schools/#{@school.id}/orders/new"**
end
end