多对关联隐藏字段编辑问题。相对回报率

发布于 2024-08-16 21:45:58 字数 1844 浏览 12 评论 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 的角色”

接下来,我尝试将 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 技术交流群。

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

发布评论

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

评论(4

不…忘初心 2024-08-23 21:45:58

如果你想提交一个值数组,我相信你可以这样做:

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

然后Rails应该将具有相同名称'role_ids[]'的每个隐藏字段的值合并到一个数组中,给你一些东西像这样:

Parameters: {"commit"=>"Update" ... "role_ids"=>["2", "4", "5"], ... }}

If you want to submit an array of values I believe you can do something like this:

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

Rails should then merge the value of each hidden field with the same name 'role_ids[]' into an array giving you something like this:

Parameters: {"commit"=>"Update" ... "role_ids"=>["2", "4", "5"], ... }}
述情 2024-08-23 21:45:58

接受的答案对我不起作用。如果您使用 hidden_​​field_tag 但不使用 f.hidden_​​field,则该方法有效。我需要求助于

<%= f.hidden_field 'role_ids][', value: role.id %>

注意反括号。

The accepted answer doesn't work for me. It works if you're using hidden_field_tag but not with f.hidden_field. I need to resort to

<%= f.hidden_field 'role_ids][', value: role.id %>

Note the reversed bracket.

我不是你的备胎 2024-08-23 21:45:58

如果你只传递 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(',')}">

盗心人 2024-08-23 21:45:58

您可以在控制器编辑操作(空中代码)中完成这项工作:

@role_ids = @user.roles.map(&:id).join(";")

在表单中:

<%= f.hidden_field, 'role_ids', :value => @role_ids %>

然后在更新操作中:

roles = params[:user][:role_ids].split(";")

You could do the work in the controller edit action (air code):

@role_ids = @user.roles.map(&:id).join(";")

In the form:

<%= f.hidden_field, 'role_ids', :value => @role_ids %>

Then in the update action:

roles = params[:user][:role_ids].split(";")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文