0

我有以下型号

角色.rb

class Role < ActiveRecord::Base
  attr_accessible :description, :user_id, :role_ids, :permission_ids, :permissions

  has_many :assignments
  has_many :users, :through => :assignments
  has_many :permissions

  validates :description, presence: true, 
                          uniqueness: true

end

权限.rb

class Permission < ActiveRecord::Base
  attr_accessible :action, :role_id, :subject_class, :subject_id, :role

  belongs_to :role, :polymorphic => true
end

我的 roles_controller.rb 如下:

  def new
    prepare
    @role = Role.new
    # @permission = @role.permissions.build


    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @role }
    end
  end

  def create
    @role = Role.new(params[:role])
    @permissions = @role.permissions.build(params[:permissions])
    p params

    respond_to do |format|
      if @role.save
        format.html { redirect_to @role, notice: 'Role was successfully created.' }
        format.json { render json: @role, status: :created, location: @role }
      else
        format.html { render action: "new" }
        format.json { render json: @role.errors, status: :unprocessable_entity }
      end
    end
  end

当我提交这个时,我得到以下控制台输出。

Started POST "/roles" for 127.0.0.1 at 2012-10-01 11:26:03 +0100
Processing by RolesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"3uZ47itmgzUSs83Iq9+2saJn6Y9+Y9tlSDgaRskh9pw=", "role"=>{"description"=>"T1"}, "permission"=>{"subject_class"=>"CancanPermission"}, "commit"=>"Create Role"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'gn668LQGNlLl6HiwPf8DRQ' LIMIT 1
  Role Load (0.1ms)  SELECT "roles".* FROM "roles" INNER JOIN "assignments" ON "roles"."id" = "assignments"."role_id" WHERE "assignments"."user_id" = 13
   (0.0ms)  begin transaction
  Role Exists (0.1ms)  SELECT 1 FROM "roles" WHERE "roles"."description" = 'T1' LIMIT 1
  SQL (0.4ms)  INSERT INTO "roles" ("created_at", "description", "updated_at") VALUES (?, ?, ?)  [["created_at", Mon, 01 Oct 2012 10:26:03 UTC +00:00], ["description", "T1"], ["updated_at", Mon, 01 Oct 2012 10:26:03 UTC +00:00]]
  SQL (0.2ms)  INSERT INTO "permissions" ("action", "created_at", "role_id", "subject_class", "subject_id", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["action", nil], ["created_at", Mon, 01 Oct 2012 10:26:03 UTC +00:00], ["role_id", 47], ["subject_class", nil], ["subject_id", nil], ["updated_at", Mon, 01 Oct 2012 10:26:03 UTC +00:00]]
   (1.7ms)  commit transaction
Redirected to http://localhost:3005/roles/47
Completed 302 Found in 19ms (ActiveRecord: 3.0ms)


Started GET "/roles/47" for 127.0.0.1 at 2012-10-01 11:26:03 +0100
Processing by RolesController#show as HTML
  Parameters: {"id"=>"47"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'gn668LQGNlLl6HiwPf8DRQ' LIMIT 1
  Role Load (0.1ms)  SELECT "roles".* FROM "roles" INNER JOIN "assignments" ON "roles"."id" = "assignments"."role_id" WHERE "assignments"."user_id" = 13
  Role Load (0.1ms)  SELECT "roles".* FROM "roles" WHERE "roles"."id" = ? LIMIT 1  [["id", "47"]]
  Rendered roles/show.html.erb within layouts/application (0.7ms)
  Rendered layouts/_header.html.erb (1.5ms)
  Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 9ms (Views: 6.2ms | ActiveRecord: 0.4ms)

我在权限中的 subject_class 参数有一个值 [ CancanPermission],但它没有插入权限表中。我只从构建关联中获取 role_id。

有人可以指导我做错什么吗?

4

1 回答 1

0

这是我在进行更多谷歌搜索和 SO 搜索后所做的。

我在 roles_controller.rb 的创建操作中更改了以下行,如下所示:

从:

@permissions = @role.permissions.build(params[:permissions])

至:

@permissions = @role.permissions.build(params[:permission])

嘿,很快,它奏效了!

但是在我的编辑中使用同一行,我在权限表中获得了新条目,但是更新了版本。那么编辑/更新的语法是什么?

于 2012-10-01T11:32:30.153 回答