rails 3 undefined方法`update_attributes'代表nil:NilClass

时间:2012-02-01 04:17:27

标签: ruby-on-rails forms undefined update-attributes

我是Ruby和rails的新手。我正在做一个更新的表单:硬件数据库中的所有者值。在使用自定义视图名称transfer.html.erb。我的表格代码显示在下面。

<%= form_for @hardware, :url => do_transfer_path do |f| %>

这是我在hardwares_tranfers_controller.rb中的自定义控制器

def transfer
  @hardware = Hardware.find(params[:id])
   if current_user.user_lvl == 0
   @users = User.order(:user_lvl)
   else
   @users = User.where(:refer => current_user.id)

  respond_to do |format|
   format.html # index.html.erb
   format.json { render json: @hardwares }
   end
  end
end


def do_transfer
#    transfer_user = User.where(:id => @hardware.owner).email
#      TransferLog.create([:user_id => current_user.id, :hardware_id => @hardware.id])
  if @hardware.update_attributes(params[:hardware])
    format.html { redirect_to users_url, notice: "Hardware was successfully transfer"}
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @hardware.errors, status: :unprocessable_entity }
  end
end

我得到的错误是     HardwaresTransfersController#do_transfer中的NoMethodError     未定义的方法update_attributes' for nil:NilClass Aplication Traces app/controllers/hardwares_transfers_controller.rb:23:in do_transfer'

参数

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"vkwKyuS4eviRN1ogrpOcGh1Y5OBNX0wiBzpV8taNkq0=",
 "hardware"=>{"owner"=>"3"},
 "commit"=>"Transfer Hardware"}

my routes.rb

match "hardwares_transfers/" => "hardwares_transfers#do_transfer" , :as => :do_transfer
get "hardwares_transfers/:id/transfer" => "hardwares_transfers#transfer", :as => :transfer_hardwares

rake routes&gt;

       do_transfer        /hardwares_transfers(.:format)              hardwares_transfers#do_transfer
transfer_hardwares GET    /hardwares_transfers/:id/transfer(.:format) hardwares_transfers#transfer
         hardwares GET    /hardwares(.:format)                        hardwares#index
                   POST   /hardwares(.:format)                        hardwares#create
      new_hardware GET    /hardwares/new(.:format)                    hardwares#new
     edit_hardware GET    /hardwares/:id/edit(.:format)               hardwares#edit
          hardware GET    /hardwares/:id(.:format)                    hardwares#show
                   PUT    /hardwares/:id(.:format)                    hardwares#update
                   DELETE /hardwares/:id(.:format)                    hardwares#destroy

3 个答案:

答案 0 :(得分:1)

你需要

def do_transfer
  @hardware = Hardware.find(params[:id])
  ...
end

我还要借此机会指出,您可能会以更REST的方式构建此框架

答案 1 :(得分:0)

找到硬件项目:

def do_transfer
  @hardware = Hardware.find(params[:id])
  ...
end

并更改route.rb:

match "hardwares_transfers/:id" => "hardwares_transfers#do_transfer" , :as => :do_transfer

答案 2 :(得分:-1)

错误为NoMethodError in HardwaresTransfersController#do_transfer
未定义的方法update_attributes'为nil:NilClass。

正如错误所说,Rails试图在nil:NilClass上调用update_attributes,这意味着你的@hardware对象为零。
所以,你应该通过

初始化它
@hardware = Hardware.find(params[:id])

您应该始终查看错误,并考虑一下为什么会出现此错误,它会帮助您更好地学习。