我希望有人可以帮助我解决这个问题。我正在使用fields_for DateOrder.new创建一个nested_form_for Order.new。
在我的orders_controller中,我在'new'和'create'动作中定义了@school,这对order / new和order / show都很重要。然而,Rails似乎没有找到具有参数的学校。
提交表单时出错: OrdersController中的ActiveRecord :: RecordNotFound #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
订单/ 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'
答案 0 :(得分:0)
在查询中,您传递了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。
答案 1 :(得分:0)
在params
哈希中,school_id
嵌套在order
密钥下,因此您需要使用params[:order][:school_id]
访问它。现在,params[:school_id]
将返回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
时,您会在params[:order][:school_id]
操作中收到错误消息。
备份一下,您只需在create
操作中将其更改为new
即可。对于params[:school_id]
操作,您可以将其保留为var modSearchString = "foo bar";
var synonym = "bar";
if(modSearchString.indexOf(synonym) > -1){
//modSearchString contains synonym
}else{
//modSearchString does not contain synonym
}
。
答案 2 :(得分:0)
刚刚解决了这个问题。
正如两位评论者指出的那样,我开始使用:para_中的school_id,我的创建动作:school_id在:order param中。当我重定向有缺陷的尝试创建:订单时,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