Ruby on Rails:访问哈希映射的表单元素
我需要从表单访问哈希映射值。这是我看到的转储表单元素的内容,但我不确定如何在控制器中访问它们:
{"Cart"=>{"exclude_discount"=>"1",
"only_one_product"=>"0",
"include_surcharge"=>"1",
"include_timesheet_date"=>"1"}}
我尝试使用
params[:Cart[only_one_product]]
and params[:Cart[:only_one_product]]
and params[:Cart["only_one_product"]]
“一切失败”在控制器中访问它们。非常感谢任何快速帮助。
I need to access a hashmapped value from a form. Here is what I see I dump the form elements but I am NOT sure how I can access them in controller:
{"Cart"=>{"exclude_discount"=>"1",
"only_one_product"=>"0",
"include_surcharge"=>"1",
"include_timesheet_date"=>"1"}}
I tried to access them in controller using
params[:Cart[only_one_product]]
and params[:Cart[:only_one_product]]
and params[:Cart["only_one_product"]]
Everything fails. Any quick help is really appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你想要
params[:Cart][:exclude_discount]
因为它是哈希的哈希,所以你需要首先获取外层哈希 'params[:Cart]' 的元素,然后获取内层哈希该哈希的元素
[:exclude_discount]
。you want
params[:Cart][:exclude_discount]
Since it is a hash of a hash, you need to first get the element of the outer hash 'params[:Cart]', then get the inner element of that hash,
[:exclude_discount]
.是访问它的正确方法。
is the right way to access it.
长款
短款
Long style
Short style