多对关联隐藏字段编辑问题。相对回报率
我正在尝试将数组传递到隐藏字段中。
以下用户有 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 的角色”
接下来,我尝试将 users/edit.html.erb 编辑为:
<%= f.hidden_field :role_ids, :value => @user.roles.map(&:id).join(", ") %>
产生我认为是一个字符串:“role_ids”=>“2, 4”(所需输出:“role_ids”=>[“2”, “4”])
当我尝试使用循环时:
<% for role in Role.find(:all) %>
<%= f.hidden_field :role_ids, :value => @user.roles.map(&:id).join(",").to_a %>
<% end %>
又产生了另一个字符串:
Parameters: {"commit"=>"Update", "action"=>"update", "_method"=>"put", "authenticity_token"=>"84Fvxxo3ao8Fl+aytOFInrLatUpslfAJggleEyy4wyI=", "id"=>"5", "controller"=>"users", "user"=>{"password_confirmation"=>"[FILTERED]", "role_ids"=>"2, 4, 5", "password"=>"[FILTERED]", "login"=>"lesa", "email"=>"[email protected]"}}
有什么建议吗?这一直是一个令人困惑的问题。
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"
Next I attempted to edit my users/edit.html.erb to:
<%= f.hidden_field :role_ids, :value => @user.roles.map(&:id).join(", ") %>
Yielding what I believe is a string: "role_ids"=>"2, 4" (Required Output: "role_ids"=>["2", "4"])
When I attempted using a loop:
<% for role in Role.find(:all) %>
<%= f.hidden_field :role_ids, :value => @user.roles.map(&:id).join(",").to_a %>
<% end %>
Yet another string resulted:
Parameters: {"commit"=>"Update", "action"=>"update", "_method"=>"put", "authenticity_token"=>"84Fvxxo3ao8Fl+aytOFInrLatUpslfAJggleEyy4wyI=", "id"=>"5", "controller"=>"users", "user"=>{"password_confirmation"=>"[FILTERED]", "role_ids"=>"2, 4, 5", "password"=>"[FILTERED]", "login"=>"lesa", "email"=>"[email protected]"}}
Any suggestions? This has been a perplexing problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你想提交一个值数组,我相信你可以这样做:
然后Rails应该将具有相同名称
'role_ids[]'
的每个隐藏字段的值合并到一个数组中,给你一些东西像这样:If you want to submit an array of values I believe you can do something like this:
Rails should then merge the value of each hidden field with the same name
'role_ids[]'
into an array giving you something like this:接受的答案对我不起作用。如果您使用
hidden_field_tag
但不使用f.hidden_field
,则该方法有效。我需要求助于注意反括号。
The accepted answer doesn't work for me. It works if you're using
hidden_field_tag
but not withf.hidden_field
. I need to resort toNote the reversed bracket.
如果你只传递 u.roles.map(&:id)[0] 会发生什么?值=2吗?也许该值不接受多值,或者您应该通过
<%= f.hidden_field :role_ids, :value=>“#{u.roles.map(&:id).join(',')}">
if you pass just the u.roles.map(&:id)[0] what happens? is the value = 2? Maybe the value don't accept multivalue or maybe you should pass
<%= f.hidden_field :role_ids, :value=>“#{u.roles.map(&:id).join(',')}">
您可以在控制器编辑操作(空中代码)中完成这项工作:
在表单中:
然后在更新操作中:
You could do the work in the controller edit action (air code):
In the form:
Then in the update action: