Question

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

Was it helpful?

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top