开发者

In the update action a NoMethodError is thrown

开发者 https://www.devze.com 2022-12-16 02:25 出处:网络
This is my source code def update @recipe = Recipe.find(params[:id]) respond_to do |format| if @recipe.update_attributes(params[:recipe])

This is my source code

 def update
    @recipe = Recipe.find(params[:id])

    respond_to do |format|
      if @recipe.update_attributes(params[:recipe])
        format.html {redirect_to :action => "edit" }
      end
    end
  end

I get an error on this line

respond_to do |format|

and the error message is "You have a nil object when you didn't expect it. The error occurred while evaluating nil.call".

The five lines from the stack trace are as follows

/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/mime_responds.rb:175:in `respond'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/mime_responds.rb:173:in `each'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/mime_responds.rb:173:in `respond'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/mime_responds.rb:107:in `respond_to'
/Path from my ma开发者_开发知识库chine to the app/app/controllers/recipes_controller.rb:43:in `update'

I have no idea on how to debug this and I cannot understand how can this error be raised.

Any help is truly appreciated.

Thanks


If you aren't responding to non-html clients, you don't have to use the respond_to.

Try changing the method to:

  if @recipe.update_attributes(params[:recipe])
   redirect_to :action => "edit"
  end

If that works, the error looks like it is somewhere in the mime type configuration of your app.


This cryptic error appears when you don't use the yieled format object. In fact, you really should do something when the update_attributes call fails, for example rendering the edit template:

  def update
    @recipe = Recipe.find(params[:id])

    respond_to do |format|
      if @recipe.update_attributes(params[:recipe])
        format.html { redirect_to [:edit, @recipe] }
      else 
        format.html { render :template => 'edit' }
      end
    end
  end
0

精彩评论

暂无评论...
验证码 换一张
取 消