Search code examples
ruby-on-railsactionpack

In the update action a NoMethodError is thrown


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 machine 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


Solution

  • 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.