更新失败-未定义的方法“更新属性';对于#<;ActiveRecord::关系:

更新失败-未定义的方法“更新属性';对于#<;ActiveRecord::关系:,activerecord,associations,ruby-on-rails-3.2,Activerecord,Associations,Ruby On Rails 3.2,这是根据我先前的请求,我已经回答了。但我无法理解的下一个问题是,为什么当我的参数报告一条记录时,我会收到一条消息,谷歌搜索表明我需要使用一个更新 我的更新控制器方法如下所示: def update @role = Role.find(params[:id]) logger.debug "@role: id[#{params[:id]}], #{@role}, #{@role.inspect}, #{@role.to_s}" @permission = @role.p

这是根据我先前的请求,我已经回答了。但我无法理解的下一个问题是,为什么当我的参数报告一条记录时,我会收到一条消息,谷歌搜索表明我需要使用一个更新

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

  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"}
为了帮助调试,我记录了以下内容:

@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">]
@role:id[55]、#、##
@权限:[#]
是因为一个被“包装”为数组吗?[@permission logger.debug调用的输出]。这是我应该使用update_all的指示器吗?

是,
\where()
返回一个ActiveRecord::Relation,它可以表示多个记录。您可能只想使用
@role.permissions(params[:permission]).first.update(…)
而不是update\u all,因为您只想更新一行

另外,我注意到您正在实现一个权限/角色模型。我可以为这个目的推荐一个gem吗,因为它已经被重新实现了很多次?可能是cancan/cantango之类的东西?

是的,
#where()
返回一个ActiveRecord::Relation,它可以表示多个记录。您可能只想使用
@role.permissions(params[:permission]).first.update(…)
而不是update\u all,因为您只想更新一行


另外,我注意到您正在实现一个权限/角色模型。我可以为这个目的推荐一个gem吗,因为它已经被重新实现了很多次?像cancan/cantango这样的东西,也许?

这个问题有严重的问题。你应该把标题改成可以理解的,并且试着在你的问题中加入一个特定的问题。希望是一个更好的标题。如果没有,请你提出这个问题有严重的问题。你应该把标题改成可以理解的,并且试着在你的问题中加入一个特定的问题。希望是一个更好的标题。如果没有,请你提出建议
@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">]