Question - Update error

Using Edge with scaffold_resource here. My update page is throwing a nil error.

"You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.update_attributes"

The form loads properly with all the fields as input , but when I hit update ..bam!

def update

    respond_to do |format|       if @position.update_attributes(params[:position])

        format.html { redirect_to position_url(@position) }         format.xml { render :nothing => true }       else         format.html { render :action => "edit" }         format.xml { render :xml => @position.errors.to_xml }       end     end

The log doesn't point anything out to me but I'll show it regardless in case it helps-

NoMethodError (You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.update_attributes):     /app/controllers/positions_controller.rb:93:in `update'     /vendor/rails/actionpack/lib/action_controller/mime_responds.rb:104:in `respond_to'     /app/controllers/positions_controller.rb:92:in `update'     /vendor/rails/actionpack/lib/action_controller/base.rb:1022:in `perform_action_without_filters'     /vendor/rails/actionpack/lib/action_controller/filters.rb:628:in `call_filter'     /vendor/rails/actionpack/lib/action_controller/filters.rb:634:in `call_filter'     /vendor/rails/actionpack/lib/action_controller/filters.rb:453:in `call'     /vendor/rails/actionpack/lib/action_controller/filters.rb:633:in `call_filter'     /vendor/rails/actionpack/lib/action_controller/filters.rb:615:in `perform_action_without_benchmark'     /vendor/rails/actionpack/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'     C:/INSTAN~1/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'     /vendor/rails/actionpack/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'     /vendor/rails/actionpack/lib/action_controller/rescue.rb:81:in `perform_action'     /vendor/rails/actionpack/lib/action_controller/base.rb:425:in `process_without_filters'     /vendor/rails/actionpack/lib/action_controller/filters.rb:620:in `process_without_session_management_support'     /vendor/rails/actionpack/lib/action_controller/session_management.rb:114:in `process'     /vendor/rails/actionpack/lib/action_controller/base.rb:328:in `process'     /vendor/rails/railties/lib/dispatcher.rb:41:in `dispatch'     C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel/rails.rb:73:in `process'     C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:551:in `process_client'     C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:550:in `process_client'     C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in `run'     C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in `run'     C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:in `run'     C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:956:in `run'     C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:955:in `run'     C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:127:in `run'     C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel/command.rb:199:in `run'     C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:235     /vendor/rails/activesupport/lib/active_support/dependencies.rb:357:in `load'     /vendor/rails/railties/lib/commands/servers/mongrel.rb:48     C:/INSTAN~1/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'     /vendor/rails/activesupport/lib/active_support/dependencies.rb:364:in `require'     /vendor/rails/railties/lib/commands/server.rb:39     script/server:3

I'm wondering if this is a bug - because I went back to earlier today in a test app to see what scaffold_resource generated for the update and it was like this:

def update @article = Article.find(params[:id])

    respond_to do |format|       if @article.update_attributes(params[:article])         format.html { redirect_to article_url(@article) }         format.xml { render :nothing => true }       else         format.html { render :action => "edit" }         format.xml { render :xml => @article.errors.to_xml }       end     end   end

Yet in my app it was like this:

def update

    respond_to do |format|       if @position.update_attributes(params[:position])

        format.html { redirect_to position_url(@position) }         format.xml { render :nothing => true }       else         format.html { render :action => "edit" }         format.xml { render :xml => @position.errors.to_xml }       end     end

  end

See the difference , 2nd one has no find and the if update_params on wrong line. So I've now fixed this problem but wonder if I should be report it to trac dev.rubyonrails ? I've never done that before .

Stuart

Stuart

I started getting this error when I svn upped Rails Edge the other
day as well. It's definitely slowing down my dev cycle since I'm not
getting nice error reporting in the browser any more.

(resource_navigator plugin is now broken as well, as a side-note)

Jamie

See this thread:

error page replaced with http 500 as of edge 5208 ?