【问题标题】:Rails loop through params array and save recordsRails 循环遍历 params 数组并保存记录
【发布时间】:2013-12-13 18:10:20
【问题描述】:

好的,所以我的 postgresql 数据库中有两个与我的问题有关的表:

Table: roles

Fields:

|    id    |    name    |    description    |    active   |
-----------------------------------------------------------
|          |            |                   |             |


Table: role_permissions

Fields:

|    id    |    role_id    |    permission_id    |
--------------------------------------------------
|          |               |                     |

权限本身存储在一个单独的permissions 表中,该表充当定义表。

这是我的模型关系的样子:

Role:
    has_many :role_permissions, :dependent => :destroy

Role_Permission:
    belongs_to :role

我必须创建一个新的role 的表单看起来像这样:

<%= form_for([:admin, @role], html: {role: "form"}) do |f| %>
  <%= render "admin/shared/error_messages", obj: @role %>
  <fieldset>
    <legend>General Information</legend>
    <div class="form-group">
      <%= f.label :name, "Role Name" %>
      <span class="help-block">Used to identify the role in code files.</span>
      <%= f.text_field :name, class: "form-control" %>
    </div>
    <div class="form-group">
      <%= f.label :description %>
      <span class="help-block">What is this role used for?</span>
      <%= f.text_area :description, class: "form-control" %>
    </div>
  </fieldset>
  <fieldset>
    <legend>Role Permissions</legend>
    <% @permissions.each do |p| %>
      <label>
        <%= check_box_tag "role[role_permissions][][permission_id]", p.id %>
        <%= p.name %>
      </label>
    <% end %>
  </fieldset>
  <%= f.submit "Save Role", class: "btn btn-primary" %>
  <%= link_to "Cancel", :back, class: "back-btn" %>
<% end %>

保存一个新角色(选择了一些权限)给了我这些可用的params[]

{"utf8"=>"✓",
"authenticity_token"=>"*******=",
"role"=>{"name"=>"Test Role",
"description"=>"This is a test role",
"role_permissions"=>[{"permission_id"=>"1"},
{"permission_id"=>"9"},
{"permission_id"=>"10"},
{"permission_id"=>"11"}]},
"commit"=>"Save Role"}

我可以用它来保存角色本身:

@role = Role.new(role_params)
if @role.save
  flash[:success] = "Role has been successfully created!"
  redirect_to admin_roles_path
else
  setup_subnav("users")
  flash[:danger] = "Something went wrong! Please try again."
  render :new
end

我有点困惑的部分是如何让role_permissions 参数数组保存为role_permissions 表中的单独记录。如您所见,我需要用他们的permission_id 和“父级”role_id 保存它们。

如何获取表单以保存单个 role_permissions 记录?感谢您的帮助!

【问题讨论】:

  • 您使用的是哪个版本的导轨?

标签: ruby-on-rails postgresql loops associations


【解决方案1】:

将此添加到您的角色模型

 accepts_nested_attributes_for :role_permissions

在你的控制器中

def role_params
    params.require(:role).permit(:name, :description, {:role_permissions_attributes => [:permission_id]})
end

在视图中:

<%= check_box_tag "role[role_permissions_attributes][][permission_id]", p.id %>

它应该为 param 中的 role_permissions 数组中的每个哈希创建一个 role_permission 记录

更新:

在编辑表单中

<%= check_box_tag "role[role_permissions_attributes][][permission_id]", p.id, role.role_permissions.collect(&:permission_id).include?(p.id) %>

【讨论】:

  • 我将它添加到我的 role.rb 模型中,它保存了role,但没有保存role_permissions。我需要触摸上面表格中的任何内容或roles_controller 中的create 操作吗?
  • 看到您的更新,仍然没有保存role_permissions...此行的格式正确吗? &lt;%= check_box_tag "role[role_permissions][][permission_id]", p.id %&gt;
  • 看起来,应该是role_permissions_attributes。更新了我的答案
  • 所以,它可以正确保存...但是,在我的edit 操作中,默认情况下不选中复选框。我是否需要以某种方式在这条线上手动执行此操作? &lt;%= check_box_tag "role[role_permissions_attributes][][permission_id]", p.id %&gt;
  • 感谢您的更新!楼主你厉害!我的最后一个问题,我保证。当我取消选中之前选中的内容时,我希望将其从role_permissions 数据库中删除。我是否应该从该表中删除具有 role_id 的所有内容,然后重新添加任何已检查的内容?如果我保存不止一次,它会创建重复......这也将缓解这个问题。
【解决方案2】:

添加角色

accepts_nested_attributes_for :role_permissions 和 attr_accessible :role_permissions_attributes


提示:功能相似,但方法不同

https://github.com/prasadsurase/dynacan

角色:
has_and_belongs_to_many :权限

权限:
has_and_belongs_to_many :roles

权限角色:
属于_to:角色 属于_to :权限

在视图中

<%= check_box_tag "permissions[]", role_permission.id, @role_permissions.include?(role_permission.id), { array: true} %>

【讨论】:

  • 我的应用实际上使用了 rails 4,所以 attr_accessible :role_permissions_attributes 无法工作。
猜你喜欢
  • 1970-01-01
  • 2016-04-27
  • 2015-02-23
  • 2016-10-05
  • 1970-01-01
  • 1970-01-01
  • 2019-04-27
  • 2017-08-16
相关资源
最近更新 更多