控制器中的更新没有ID错误

时间:2011-09-19 14:26:33

标签: ruby-on-rails

这是一段代码:

  def update
    @category = Category.find(params[:id])

    if @category.update_attributes(params[:category], :as => :roles_update)  
      @category = Category.find[params[:id]]
      render 'show', :notice => 'Category was successfully updated'
    else
      @categories = Category.all
      render 'index'
    end 
  end

执行上述操作时出错:

  Couldn't find Category without an ID

但是参数dump中有类别id(= 1):

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"aFk+bcHKA3zwck1PgrNOwVvZfeXtsT57ywHGiB4odDY=",
 "category"=>{"description"=>"audio/video",
 "active"=>"1"},
 "commit"=>"Update Category",
 "id"=>"1"}

如果在IF循环中注释掉#@ category = Category.find [params [:id]],代码将不会出错。但是rspect给出了错误:ActiveRecord :: RecordNotFound:

有关此问题的任何建议吗?感谢

1 个答案:

答案 0 :(得分:1)

 @category = Category.find[params[:id]] 

应该是:

 @category = Category.find(params[:id])