将一个数组传递到hidden_field ROR。

时间:2021-07-05 19:40:36

I'm trying to pass an array into a hidden_field.

我试图将数组传递给一个hidden_field。

The following User has 3 roles [2,4,5]

以下用户有3个角色[2,4,5]

>> u = User.find_by_login("lesa")
=> #<User id: 5, login: "lesa", email: "lesa.beaupry@gmail.com", crypted_password: "0f2776e68f1054a2678ad69a3b28e35ad9f42078", salt: "f02ef9e00d16f1b9f82dfcc488fdf96bf5aab4a8", created_at: "2009-12-29 15:15:51", updated_at: "2010-01-06 06:27:16", remember_token: nil, remember_token_expires_at: nil>
>> u.roles.map(&:id)
=> [2, 4, 5]

Users/edit.html.erb

用户/ edit.html.erb

<% form_for @user do |f| -%>
<%= f.hidden_field :role_ids, :value => @user.roles.map(&:id) %>

When I submit my edit form, I receive an error: ActiveRecord::RecordNotFound in UsersController#update "Couldn't find Role with ID=245"

当我提交编辑表单时,我收到一个错误:ActiveRecord::RecordNotFound在UsersController#update中“找不到ID=245的角色”

How can I pass an array into the hidden_field?

如何将数组传递到hidden_field?

6 个解决方案

#1


82  

I would use this technique.

我会用这个技巧。

<% @user.roles.each do |role| %>
    <%= f.hidden_field :role_ids, :multiple => true, :value => role.id %>
<% end %>

:multiple adds both the [] to the end of the name attribute and multiple="multiple" to the input element, which is ideal. Rails uses this internally when it generates inputs for arrays, such as in fields_for.

:multiple将[]添加到name属性的末尾,multi =“multiple”添加到input元素,这是理想的。Rails在为数组(如fields_for)生成输入时在内部使用此功能。

Unfortunately it is not well-documented. I'm going to look into fixing this.

不幸的是,它没有充分的文档记录。我要去解决这个问题。

#2


13  

The only thing that works for me (Rails 3.1) is using hidden_field_tag:

对我来说唯一有效的方法(Rails 3.1)就是使用hidden_field_tag:

<% @users.roles.each do |role| %>
    <%= hidden_field_tag "user_role_ids[]", role.id %>
<% end %> 

#3


4  

I think it still will be helpful for people like me, who googled this tread asking the same question. I came up with the next little hack:

我认为这对像我这样的人还是有帮助的,他们在谷歌上搜索了这段视频,问了同样的问题。我想到了下一个小窍门:

<%= f.hidden_field "role_ids][", { :id => "role_ids", :value => [] } %>

(pay attention to the reversed brackets in 'object_name' argument for hidden field).

(注意隐藏字段的“object_name”参数中的反括号)。

#4


2  

Try:

试一试:

 <% @user.roles.each_with_index do |role| %>
    <%= f.hidden_field "role_ids[]", :value => role.id %>
 <% end %>

#5


1  

using Rails 4.2.6

使用Rails 4.2.6

I was able to use

我可以使用。

<% @user.roles.each do |role|
  <%= f.hidden_field :role_ids, :id => role.id, :value => role.id, :multiple => true %>
<% end %>

which rendered:

呈现:

<input id="12345" value="12345" multiple="multiple" type="hidden" name="role_form[role_ids][]">

trying to use hidden_field_tag or the 'role_ids[]' the param details were not being included in my form_params.

尝试使用hidden_field_tag或'role_ids[]', param细节没有包含在form_params中。

This didn't work:

这不起作用:

<% @user.roles.each do |role|
  <%= hidden_field_tag 'role_ids[]', role %>
<% end %>

because it renders:

因为它呈现:

<input type="hidden" name="role_ids[]" id="role_ids_" value="12345">

and is seen by the controller outside of the form params.

在窗体params之外的控制器可以看到。

#6


0  

try with:

试一试:

<%= f.hidden_field :role_ids, :value => @user.roles.map(&:id).join(", ") %>

#1


82  

I would use this technique.

我会用这个技巧。

<% @user.roles.each do |role| %>
    <%= f.hidden_field :role_ids, :multiple => true, :value => role.id %>
<% end %>

:multiple adds both the [] to the end of the name attribute and multiple="multiple" to the input element, which is ideal. Rails uses this internally when it generates inputs for arrays, such as in fields_for.

:multiple将[]添加到name属性的末尾,multi =“multiple”添加到input元素,这是理想的。Rails在为数组(如fields_for)生成输入时在内部使用此功能。

Unfortunately it is not well-documented. I'm going to look into fixing this.

不幸的是,它没有充分的文档记录。我要去解决这个问题。

#2


13  

The only thing that works for me (Rails 3.1) is using hidden_field_tag:

对我来说唯一有效的方法(Rails 3.1)就是使用hidden_field_tag:

<% @users.roles.each do |role| %>
    <%= hidden_field_tag "user_role_ids[]", role.id %>
<% end %> 

#3


4  

I think it still will be helpful for people like me, who googled this tread asking the same question. I came up with the next little hack:

我认为这对像我这样的人还是有帮助的,他们在谷歌上搜索了这段视频,问了同样的问题。我想到了下一个小窍门:

<%= f.hidden_field "role_ids][", { :id => "role_ids", :value => [] } %>

(pay attention to the reversed brackets in 'object_name' argument for hidden field).

(注意隐藏字段的“object_name”参数中的反括号)。

#4


2  

Try:

试一试:

 <% @user.roles.each_with_index do |role| %>
    <%= f.hidden_field "role_ids[]", :value => role.id %>
 <% end %>

#5


1  

using Rails 4.2.6

使用Rails 4.2.6

I was able to use

我可以使用。

<% @user.roles.each do |role|
  <%= f.hidden_field :role_ids, :id => role.id, :value => role.id, :multiple => true %>
<% end %>

which rendered:

呈现:

<input id="12345" value="12345" multiple="multiple" type="hidden" name="role_form[role_ids][]">

trying to use hidden_field_tag or the 'role_ids[]' the param details were not being included in my form_params.

尝试使用hidden_field_tag或'role_ids[]', param细节没有包含在form_params中。

This didn't work:

这不起作用:

<% @user.roles.each do |role|
  <%= hidden_field_tag 'role_ids[]', role %>
<% end %>

because it renders:

因为它呈现:

<input type="hidden" name="role_ids[]" id="role_ids_" value="12345">

and is seen by the controller outside of the form params.

在窗体params之外的控制器可以看到。

#6


0  

try with:

试一试:

<%= f.hidden_field :role_ids, :value => @user.roles.map(&:id).join(", ") %>

相关文章