has_many / belongs_to构建关联 - 为什么insert参数为空白?

时间:2012-10-01 10:37:13

标签: ruby-on-rails-3 activerecord associations

我有以下型号

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

permission.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中的subject_class参数的值为[CancanPermission],但它不会插入权限表中。我只从构建关联中获取role_id。

有人可以指导我做错了吗?

1 个答案:

答案 0 :(得分:0)

这是我在更多谷歌搜索后做的事情。搜索。

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

自:

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

要:

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

嘿presto,它有效!

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

相关问题