2012-10-01 36 views
0

我有以下型號has_many/belongs_to構建關聯 - 爲什麼參數空白插入語句?

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_class參數有一個值CancanPermission],但它不會插入權限表。我只從build關聯中獲得role_id。

有人可以指導我做什麼我做錯了嗎?

回答

0

以下是我在搜索後使用了一些更多的Google搜索&。

我改變了以下線在我創建行動在roles_controller.rb如下:

來源:

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

要:

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

變戲法似的,它的工作!

但在我的編輯中使用相同的行,我得到權限表中的新條目,但更新版本。那麼編輯/更新的語法是什麼?