0

私は次のモデルを持っています

role.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 の create アクションの次の行を次のように変更しました。

から:

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

に:

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

早速、うまくいきました!

しかし、編集で同じ行を使用すると、権限テーブルに新しいエントリが表示されますが、更新されたバージョンになります。では、編集/更新の構文は何ですか?

于 2012-10-01T11:32:30.153 に答える