Rails循环遍历params数组并保存记录

时间:2013-12-13 18:10:20

标签: ruby-on-rails postgresql loops associations

好的,所以我的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 param数组保存为role_permissions表中的单个记录。如您所见,我需要使用permission_id和“父”role_id保存它们。

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

2 个答案:

答案 0 :(得分: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) %>

答案 1 :(得分:0)

添加角色

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:角色

PermissionsRoles:
belongs_to:角色 belongs_to:permission

在视图中

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