Ruby-on-rails 将数组传递给 hidden_​​field ROR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2015204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:06:45  来源:igfitidea点击:

Passing an array into hidden_field ROR

ruby-on-railsarrayshidden-field

提问by JZ.

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: "[email protected]", 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 in UsersController#update“找不到 ID=245 的角色

How can I pass an array into the hidden_field?

如何将数组传递到 hidden_​​field 中?

回答by Grant Hutchins

I would use this technique.

我会使用这种技术。

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

:multipleadds 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 属性的末尾和multiple="multiple"input 元素,这是理想的。Rails 在为数组生成输入时在内部使用它,例如 in fields_for

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

不幸的是,它没有得到很好的记录。我要研究解决这个问题。

回答by Matteo Melani

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 %> 

回答by jamuraa

Try:

尝试:

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

回答by mendokusai

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_tagor the 'role_ids[]'the param details were not being included in my form_params.

尝试使用hidden_field_tag'role_ids[]'参数详细信息未包含在我的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 outsideof the form params.

并且被表单参数之外的控制器看到。

回答by makevoid

try with:

尝试:

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

edit: note - you'll need to do ids.split(", ")in your controller to convert them from a string into an array

编辑:注意 - 您需要ids.split(", ")在控制器中将它们从字符串转换为数组

回答by richardsonae

I realize the original question involved a Rails form_forform, but I just encountered this problem while trying adding a hidden field to a form_tagform. The solution is slightly different.

我意识到最初的问题涉及一个 Railsform_for表单,但我在尝试向form_tag表单添加隐藏字段时遇到了这个问题。解决方案略有不同。

The hidden_field_tagkept converting my ids to a space-separated string, like "1 2 3", not the array of ["1", "2", "3"]that I wanted. According to the docs, this is a known problem with hidden_field_tag. This is the approach that ended up working for my Rails 6 form_tagform:

hidden_field_tag不停转换我的ID,以一个空格分隔的字符串,如"1 2 3",而不是数组的["1", "2", "3"],我想。根据文档,这是hidden_field_tag. 这是最终适用于我的 Rails 6form_tag表单的方法:

In the view:

在视图中:

<%= hidden_field_tag :role_ids, my_role_ids_array %>

In the controller:

在控制器中:

role_ids = params[:role_ids].split(' ').map(&:to_i)