更新失败 - 为#未定义的方法`update_attributes方法(update fail -

2019-10-17 06:45发布

这从我以前的我回答如下要求上。 但我的下一个问题,我无法理解的就是为什么当我的参数报告的一个记录我得到一个消息,谷歌搜索/ SO搜索建议我需要使用update_all。

的has_many / belongs_to的建设协会-为什么是参数为空insert语句?

我的更新控制器的方法如下:

  def update
    @role = Role.find(params[:id])
    logger.debug "@role: id[#{params[:id]}], #{@role}, #{@role.inspect}, #{@role.to_s}"
    @permission = @role.permissions(params[:permission])
    logger.debug "@permission: #{@permission.inspect}"

    respond_to do |format|
      if @role.update_attributes(params[:role]) && @permission.update_attributes(params[:permission])
        format.html { redirect_to @role, notice: 'Role was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @role.errors, status: :unprocessable_entity }
      end
    end
  end

我收到以下错误信息:

 NoMethodError in RolesController#update

undefined method `update_attributes' for #<ActiveRecord::Relation:0x007fb2116b5178>

Rails.root: /Users/railsdev/Development/railsprojects/Genie/BitBucket-Repos/Genie-v3-3
Application Trace | Framework Trace | Full Trace

app/controllers/roles_controller.rb:75:in `block in update'
app/controllers/roles_controller.rb:74:in `update'

Request

Parameters:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"3uZ47itmgzUSs83Iq9+2saJn6Y9+Y9tlSDgaRskh9pw=",
 "role"=>{"description"=>"T1"},
 "permission"=>{"subject_class"=>"User"},
 "commit"=>"Update Role",
 "id"=>"55"}

谷歌搜索和SO搜索表明,我可能需要使用[ update_all ]代替。 但是,为什么当我只有一个单一的记录是这样的话?

下面是我的控制台输出。

Started PUT "/roles/55" for 127.0.0.1 at 2012-10-01 22:12:16 +0100
Processing by RolesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"3uZ47itmgzUSs83Iq9+2saJn6Y9+Y9tlSDgaRskh9pw=", "role"=>{"description"=>"T1"}, "permission"=>{"subject_class"=>"User"}, "commit"=>"Update Role", "id"=>"55"}
  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", "55"]]
@role: id[55], #<Role:0x007fb2137e3200>, #<Role id: 55, description: "T1", created_at: "2012-10-01 21:11:43", updated_at: "2012-10-01 21:11:43">, #<Role:0x007fb2137e3200>
  Permission Load (0.1ms)  SELECT "permissions".* FROM "permissions" WHERE "permissions"."role_id" = 55
@permission: [#<Permission id: 32, role_id: 55, subject_class: "Diagnosis", action: nil, subject_id: nil, created_at: "2012-10-01 21:11:43", updated_at: "2012-10-01 21:11:43">]
  Permission Load (0.1ms)  SELECT "permissions".* FROM "permissions" WHERE "permissions"."role_id" = 55
Completed 500 Internal Server Error in 5ms

NoMethodError (undefined method `update_attributes' for #<ActiveRecord::Relation:0x007fb2116b5178>):
  app/controllers/roles_controller.rb:75:in `block in update'
  app/controllers/roles_controller.rb:74:in `update'

为了帮助我调试我已经登录了以下内容:

@role: id[55], #<Role:0x007fb2137e3200>, #<Role id: 55, description: "T1", created_at: "2012-10-01 21:11:43", updated_at: "2012-10-01 21:11:43">, #<Role:0x007fb2137e3200>

@permission: [#<Permission id: 32, role_id: 55, subject_class: "Diagnosis", action: nil, subject_id: nil, created_at: "2012-10-01 21:11:43", updated_at: "2012-10-01 21:11:43">]

是不是因为一个“包裹”作为一个数组? [在@permission logger.debug呼叫的输出。 这是我应该使用update_all指标?

Answer 1:

是的, #where()返回一个ActiveRecord ::关系,这可以代表多个记录。 你可能只是想用@role.permissions(params[:permission]).first.update(...)而不是update_all,因为你只打算更新一行。

另外,我注意到,你在执行权限/榜样。 我建议用于此目的的宝石,因为这已经被重新实现了多少次? 像康康舞/ cantango,也许?



文章来源: update fail - undefined method `update_attributes' for #