将数组传递到hidden_​​field ROR

发布于 2024-08-16 16:07:28 字数 901 浏览 3 评论 0原文

我正在尝试将数组传递到隐藏字段中。

以下用户有 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

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

当我提交编辑表单时,我收到错误: ActiveRecord::RecordNotFound in UsersController#update "无法找到角色ID=245"

如何将数组传递到隐藏字段?

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

The following User has 3 roles [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

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

How can I pass an array into the hidden_field?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

桃酥萝莉 2024-08-23 16:07:28

我会使用这种技术。

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

:multiple[] 添加到 name 属性的末尾,并将 multiple="multiple" 添加到输入元素,这是理想的。 Rails 在生成数组输入时会在内部使用它,例如在 fields_for 中。

不幸的是,它没有详细记录。我要考虑解决这个问题。

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.

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

月下客 2024-08-23 16:07:28

唯一对我有用的东西(Rails 3.1)是使用hidden_​​field_tag:

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

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

<% @users.roles.each do |role| %>
    <%= hidden_field_tag "user_role_ids[]", role.id %>
<% end %> 
勿忘初心 2024-08-23 16:07:28

尝试:

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

Try:

 <% @user.roles.each_with_index do |role| %>
    <%= f.hidden_field "role_ids[]", :value => role.id %>
 <% end %>
多像笑话 2024-08-23 16:07:28

使用 Rails 4.2.6

我能够使用

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

渲染的:

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

尝试使用 hidden_​​field_tag'role_ids[]' 参数详细信息未包含在我的 中表单参数

这不起作用:

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

因为它呈现:

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

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

using 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.

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.

夏雨凉 2024-08-23 16:07:28

尝试使用:

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

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

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

紫轩蝶泪 2024-08-23 16:07:28

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

hidden_​​field_tag 不断将我的 ID 转换为空格分隔的字符串,例如 "1 2 3",而不是 ["1", "2", “3”] 我想要的。根据文档,这是 hidden_​​field_tag< 的一个已知问题/代码>。这是最终适用于我的 Rails 6 form_tag 表单的方法:

在视图中:

<%= hidden_field_tag :role_ids, my_role_ids_array %>

在控制器中:

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

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

The hidden_field_tag kept 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_tag form:

In the view:

<%= hidden_field_tag :role_ids, my_role_ids_array %>

In the controller:

role_ids = params[:role_ids].split(' ').map(&:to_i)
自由如风 2024-08-23 16:07:28

可以通过以下方式解决此类问题:

      <% params[:filter].each do |filter| %>
        <%= hidden_field_tag 'filter[]', filter %>
      <% end %>

Could solve this kind of issue with :

      <% params[:filter].each do |filter| %>
        <%= hidden_field_tag 'filter[]', filter %>
      <% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文