如何使用check_box_tag传递给Rails中的params hash?
How to use check_box_tag to pass it to the params hash in Rails?
所以我循环遍历了一系列电影角色,每个角色都有一个复选框。我想要这样,当我点击 "Select My Cast" 按钮时,我被重定向到 teams_path(我的控制器中的索引方法),以便能够获取我在数组中选择的字符的 ID在我的参数哈希中。
代码如下:
-@chars.each do |i|
=image_tag(i.image)
%br
First Name:
=i.f_name
%br
Last Name:
=i.l_name
%br
Show :
=i.show
%br
=check_box_tag 'char_ids[]', i.id
%hr
= link_to "Select My Cast", teams_path, :method => :get
我在我的控制器的索引方法中放置了一个 binding.pry 并且参数没有 char_ids 数组或任何相关信息。谢谢您的帮助;我意识到这是一个众所周知的问题。
如果您的控制器没有从您的代码片段中获取参数;最有可能是:
- 您忘记在页面上添加表单标签
- 单击那个
link_to
锚 link 并且没有提交您的表单。
因此,请确保您有 form_for
或 form_tag
设置,并确保您使用某种 "button" 而不仅仅是锚标记 (link_to
).
PS. 您不需要将 method: :get
添加到您的 link_to
,因为它是 get
默认。
PSS. 您可以在 form_for check_box
中使用 multiple: true
来获取多个值。
我刚遇到这个问题,花了一段时间才解决,所以我 post 我会 post 认真对待我的解决方案。
我有一个带有序列化哈希字段的用户模型 messaging_preferences=> { "articles"=>false, "marketing"=>false, "digest"=>false }
让我感到困惑的是需要在表单中使用 fields_for
。
<%= f.fields_for :messaging_preferences, user.messaging_preferences do |message_type| %>
Marketing emails: <%= message_type.check_box :marketing, {}, "true", "false" %><br />
Article emails: <%= message_type.check_box :articles, {}, "true", "false" %><br />
Weekly digests: <%= message_type.check_box :digest, {}, "true", "false" %><br />
<% end %>
并更新控制器中的 user_params 以反映(像这样):``
但是有了这个(以及 user_name 和电子邮件参数)生成对服务器的这个调用:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"{{token}}", "user"=>{"name"=>"Jimmy Johnny", "email"=>"spam@gmail.com", "messaging_preferences"=>{"marketing"=>"false", "articles"=>"false", "digest"=>"false"}}, "commit"=>"Update User", "id"=>"6"}
所以,这里我们发送了嵌套的参数。
所以我循环遍历了一系列电影角色,每个角色都有一个复选框。我想要这样,当我点击 "Select My Cast" 按钮时,我被重定向到 teams_path(我的控制器中的索引方法),以便能够获取我在数组中选择的字符的 ID在我的参数哈希中。
代码如下:
-@chars.each do |i|
=image_tag(i.image)
%br
First Name:
=i.f_name
%br
Last Name:
=i.l_name
%br
Show :
=i.show
%br
=check_box_tag 'char_ids[]', i.id
%hr
= link_to "Select My Cast", teams_path, :method => :get
我在我的控制器的索引方法中放置了一个 binding.pry 并且参数没有 char_ids 数组或任何相关信息。谢谢您的帮助;我意识到这是一个众所周知的问题。
如果您的控制器没有从您的代码片段中获取参数;最有可能是:
- 您忘记在页面上添加表单标签
- 单击那个
link_to
锚 link 并且没有提交您的表单。
因此,请确保您有 form_for
或 form_tag
设置,并确保您使用某种 "button" 而不仅仅是锚标记 (link_to
).
PS. 您不需要将 method: :get
添加到您的 link_to
,因为它是 get
默认。
PSS. 您可以在 form_for check_box
中使用 multiple: true
来获取多个值。
我刚遇到这个问题,花了一段时间才解决,所以我 post 我会 post 认真对待我的解决方案。
我有一个带有序列化哈希字段的用户模型 messaging_preferences=> { "articles"=>false, "marketing"=>false, "digest"=>false }
让我感到困惑的是需要在表单中使用 fields_for
。
<%= f.fields_for :messaging_preferences, user.messaging_preferences do |message_type| %>
Marketing emails: <%= message_type.check_box :marketing, {}, "true", "false" %><br />
Article emails: <%= message_type.check_box :articles, {}, "true", "false" %><br />
Weekly digests: <%= message_type.check_box :digest, {}, "true", "false" %><br />
<% end %>
并更新控制器中的 user_params 以反映(像这样):``
但是有了这个(以及 user_name 和电子邮件参数)生成对服务器的这个调用:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"{{token}}", "user"=>{"name"=>"Jimmy Johnny", "email"=>"spam@gmail.com", "messaging_preferences"=>{"marketing"=>"false", "articles"=>"false", "digest"=>"false"}}, "commit"=>"Update User", "id"=>"6"}
所以,这里我们发送了嵌套的参数。