将数组传递到hidden_field ROR
我正在尝试将数组传递到隐藏字段中。
以下用户有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我会使用这种技术。
:multiple
将[]
添加到 name 属性的末尾,并将multiple="multiple"
添加到输入元素,这是理想的。 Rails 在生成数组输入时会在内部使用它,例如在fields_for
中。不幸的是,它没有详细记录。我要考虑解决这个问题。
I would use this technique.
:multiple
adds both the[]
to the end of the name attribute andmultiple="multiple"
to the input element, which is ideal. Rails uses this internally when it generates inputs for arrays, such as infields_for
.Unfortunately it is not well-documented. I'm going to look into fixing this.
唯一对我有用的东西(Rails 3.1)是使用hidden_field_tag:
The only thing that works for me (Rails 3.1) is using hidden_field_tag:
尝试:
Try:
使用 Rails 4.2.6
我能够使用
渲染的:
尝试使用
hidden_field_tag
或'role_ids[]'
参数详细信息未包含在我的中表单参数
。这不起作用:
因为它呈现:
并被表单参数外部的控制器看到。
using Rails 4.2.6
I was able to use
which rendered:
trying to use
hidden_field_tag
or the'role_ids[]'
the param details were not being included in myform_params
.This didn't work:
because it renders:
and is seen by the controller outside of the form params.
尝试使用:
编辑:注意 - 您需要在控制器中执行
ids.split(", ")
将它们从字符串转换为数组try with:
edit: note - you'll need to do
ids.split(", ")
in your controller to convert them from a string into an array我意识到原来的问题涉及 Rails
form_for
表单,但我在尝试向form_tag
表单添加隐藏字段时遇到了这个问题。解决方案略有不同。hidden_field_tag
不断将我的 ID 转换为空格分隔的字符串,例如"1 2 3"
,而不是["1", "2", “3”]
我想要的。根据文档,这是hidden_field_tag< 的一个已知问题/代码>。这是最终适用于我的 Rails 6
form_tag
表单的方法:在视图中:
在控制器中:
I realize the original question involved a Rails
form_for
form, but I just encountered this problem while trying adding a hidden field to aform_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 withhidden_field_tag
. This is the approach that ended up working for my Rails 6form_tag
form:In the view:
In the controller:
可以通过以下方式解决此类问题:
Could solve this kind of issue with :