0
我对我的产品索引页以下:Rails的Button_to不传递模型PARAM
<%= button_to "Add", user_order_orderitems_path(user_id: current_user.id, item_id: x.id, order_id: current_user.group.current_order.id), class: "btn btn-mini" %>
,我可以从日志中看到正在我的OrderItems#拿起我的控制器确定创建行动。这看起来像:正在创建
def create
@orderitem = Orderitem.new(orderitem_params)
if @orderitem.save
redirect_to items_path
else
redirect_to items_path
end
end
private
def orderitem_params
params.require(:orderitem).permit(:user_id, :order_id, :item_id)
end
end
在button_to调用指定的PARAMS并没有显示在日志为:
Started POST "https://stackoverflow.com/users/1/orders/1/orderitems?item_id=2264" for 127.0.0.1 at 2013-07-03 22:45:24 +0100
Processing by OrderitemsController#create as HTML
Parameters: {"authenticity_token"=>"blah=", "item_id"=>"2264", "user_id"=>"1", "order_id"=>"1"}
Fnally - 问题 - 我strong_params方法无法处理这些因为我关心的三个参数不是嵌套在“Orderitem”作为关键字的散列中。我希望,我的创造行为的工作,我需要的是这样的:
Parameters: {"authenticity_token"=>"blah=", "orderitems"=>{"item_id"=>"2264", "user_id"=>"1", "order_id"=>"1"}}
,但我不能为我的生活工作如何,与button_to我能够做到这一点 - 我已经尝试了form_for,但这也失败了。我的脑袋在砖墙上刺激了两天......因此,如何将我的三个ID发布到我的OrderItemsController中,从产品的索引视图创建操作,但绕过了任何form_for或新操作?可能吗?
请让我知道如果我正在接近这种情况(添加一个项目到一个篮子)完全错误的方式。
辉煌的支持 - 感谢 - 在Rails的“最佳实践”方面,这被认为是好的做法呢?显示我在这方面的知识不足...... – BenjiBoyWick
嗯,我不这么认为,这些是来自外部的参数..并且解决方案也在这里提供https://github.com/rails/strong_parameters – AnkitG