我不太明白为什么会发生这种情况,也许你们中的某个人会……就这样吧。
我创建了一个嵌套资源:
resources :order do
resources :ordered_vehicles
end
我添加了一个link_to
动作new
并传递了order.id
类似new_order_ordered_vehicle_path(order.id)
的内容,因此页面加载得很好。问题是在我按下按钮提交选择之后。他将路径从 切换http://localhost:3000/order/3/ordered_vehicles/new
到http://localhost:3000/order/R076027535/ordered_vehicles
并显示错误Couldn't find Order with id=R076027535
……看图。
此方法在控制器中引发错误
private
def find_order
@order = Order.find(params[:order_id])
end
这是一个before_filter
.
new.html.haml
文件看起来像这样
= form_for [@order, @ordered_vehicle], html: { multipart: true } do |f|
= @order.number
%br= @order.id
= f.fields_for :vehicles do |car|
.... #some fields here
= car.submit "Save your choice"
他id
找不到的是,@order.number
但我不明白为什么要切换。
编辑:为了彻底,我将添加控制器方法:
def new
@ordered_vehicle = @order.ordered_vehicles.build(params.slice(:order_id, :vehicle_id))
end
def create
binding.pry
@ordered_vehicle = @order.ordered_vehicles.build(params.slice(:order_id, :vehicle_id))
if @ordered_vehicle.save
flash[:notice] = "Save successful."
redirect_to account_path
end
end
POST 请求(我希望这是一个,对所有这些东西来说仍然是新的):
"action_dispatch.request.formats"=>[text/html]},
@request_method="POST", @filtered_parameters={"utf8"=>"✓", "authenticity_token"=>
"Ar4vy8pqCSpA2ch0qG0qiJXAJUbNALYxm/FbuKbdzCc=", "ordered_vehicle"=>
{"vehicles"=> {"maker_id"=>"", "model_id"=>"", "year"=>"", "body"=>"", "capacity"=>"", "id"=>"1"}},
"commit"=>"Save your choice", "action"=>"create",
"controller"=>"spree/ordered_vehicles", "order_id"=>"R076027535"}, @method="POST",
@fullpath="/order/R076027535/ordered_vehicles">