Topic: Very confusing error! Why?

Hello.

I am having trouble with submitting a form to update a table in my database. I have several forms which are almost identical and all of them work perfectly. However, this last one seems to be producing an error and i cant find the reason why.

The form looks like this:

 <div id="title">
   <h1> Edit News Item...
</div>

<div id="img_content">

<form action="../update/<%= @newsitem.id %>" method="POST"">
  <input id="newsitem_id" name="newsitem[id]" size="30"
         type="hidden" value="<%= @newsitem.id %>" />
 

<p><b>Headline</b><br>
  <input id="newsitem_headline" name="newsitem[headline]" size="41"
         type="text" value="<%= @newsitem.headline %>" />
  </p>


<p><b>Date</b><br>
  <input id="newsitem_date" name="newsitem[date]" size="41"
         type="date" value="<%= @newsitem.date %>" />
  </p>
 
<p><b>Topic:</b><br>
<select name="newsitem[topic_id]">
   <% @topics.each do |topic| %>
       <option value="<%= topic.id %>"
         <%= ' selected' if topic.id == @newsitem.topic_id %>>
         <%= topic.topic_name %>
       </option>
   <% end %>
  </select></p>

  <p><b>Overview</b><br>
  <textarea class="textarea" cols="40" id="newsitem_overview" name="newsitem[overview]" rows="2" wrap="virtual"><%= @newsitem.overview %></textarea></p>


  <p><b>Overview</b><br>
  <textarea class="textarea" cols="40" id="newsitem_content" name="newsitem[content]" rows="10" wrap="virtual"><%= @newsitem.content %></textarea></p>

</div>

<div id ="buttons"
<input type="submit" value="Update" />
</div>
</form>


...the controller looks like this:

 class NewsitemsController < ApplicationController
  layout "standard"
 
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @topic = @params['topic']
    @newsitems = Newsitem.find_all
  end

  def show
    @newsitem = Newsitem.find(params[:id])
  end

  def new
    @newsitem = Newsitem.new
    @topics = Topic.find_all
  end

  def create
    @newsitem = Newsitem.new(params[:newsitem])
    @newsitem.date = Date.today
    if @newsitem.save
      flash[:notice] = 'The News item was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @newsitem = Newsitem.find(params[:id])
    @topics = Topic.find_all
  end

  def update
    @newsitem = Newsitem.find(params[:id])
    if @newsitem.update_attributes(params[:newsitem])
      flash[:notice] = 'News Item updated.'
      redirect_to :action => 'list', :id => @newsitem
    else
      render :action => 'edit'
    end
  end

  def delete
    Newsitem.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end


...the error i receive looks like this:



NoMethodError in NewsitemsController#update

undefined method `Heading' for #<Newsitem:0x24f8a3c>

RAILS_ROOT: /Users/peterroome/Sites/Everycare/Website/everycare/public/../config/..
Application Trace | Framework Trace | Full Trace

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1792:in `method_missing'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:300:in `validates_each'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:299:in `validates_each'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:794:in `run_validations'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:788:in `run_validations'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:752:in `valid_without_callbacks'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/callbacks.rb:306:in `valid?'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:723:in `save_without_transactions'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:126:in `save'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in `transaction'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:91:in `transaction'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:118:in `transaction'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:126:in `save'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1439:in `update_attributes'
#{RAILS_ROOT}/app/controllers/newsitems_controller.rb:45:in `update'

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1792:in `method_missing'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:300:in `validates_each'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:299:in `validates_each'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:794:in `run_validations'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:788:in `run_validations'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:752:in `valid_without_callbacks'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/callbacks.rb:306:in `valid?'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:723:in `save_without_transactions'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:126:in `save'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in `transaction'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:91:in `transaction'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:118:in `transaction'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:126:in `save'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1439:in `update_attributes'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in `perform_action_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in `perform_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in `process_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in `process_without_session_management_support'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in `process'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in `dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:150:in `process_request'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:54:in `process!'
/usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/fcgi.rb:600:in `each_cgi'
/usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/fcgi.rb:597:in `each_cgi'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:53:in `process!'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:23:in `process!'
/Users/peterroome/Sites/Everycare/Website/everycare/public/dispatch.fcgi:24

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1792:in `method_missing'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:300:in `validates_each'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:299:in `validates_each'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:794:in `run_validations'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:788:in `run_validations'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:752:in `valid_without_callbacks'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/callbacks.rb:306:in `valid?'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:723:in `save_without_transactions'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:126:in `save'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in `transaction'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:91:in `transaction'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:118:in `transaction'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:126:in `save'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1439:in `update_attributes'
#{RAILS_ROOT}/app/controllers/newsitems_controller.rb:45:in `update'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in `perform_action_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in `perform_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in `process_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in `process_without_session_management_support'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in `process'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in `dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:150:in `process_request'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:54:in `process!'
/usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/fcgi.rb:600:in `each_cgi'
/usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/fcgi.rb:597:in `each_cgi'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:53:in `process!'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:23:in `process!'
/Users/peterroome/Sites/Everycare/Website/everycare/public/dispatch.fcgi:24

Request

Parameters: {"id"=>"1", "newsitem"=>{"topic_id"=>"2", "headline"=>"The new everycare website", "overview"=>"The new website is finally here thanks to Ceative Consulting.", "date"=>"2006-12-07", "id"=>"1", "content"=>" A new database driven webiste that has all the features of an expensive aplication. We would like to thnk them very muchf ro all they have done.\r\n "}}

Show session dump

---
flash: !map:ActionController::Flash::FlashHash {}


Response
Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}





...i am slightly concerned about the 'undefined method `Heading''. I have searched high and low through my code for typos and other such anomalies but have yet to find anything. Any ideas would be welcomed with open arms.

Kind Regards

Pete

Re: Very confusing error! Why?

Did you do an automated find all on your entire project for the term Heading? It is strange that this error would come up if it's not mentioned anywhere in the code.

Can you post your Newsitem model? The error apparently is occurring during validation.

Railscasts - Free Ruby on Rails Screencasts

Re: Very confusing error! Why?

Ahhhh brilliant! I have spotted the error in my Model. I hadn't updated a field name change in the validation.

However, i am now having problems with my drop down menu...

NoMethodError in Newsitems#update

Showing app/views/newsitems/edit.rhtml where line #25 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.each

Extracted source (around line #25):

22:   
23: <p><b>Topic:</b><br>
24:  <select name="newsitem[topic_id]">
25:    <% @topics.each do |topic| %>
26:        <option value="<%= topic.id %>"
27:          <%= ' selected' if topic.id == @newsitem.topic_id %>>
28:          <%= topic.topic_name %>

RAILS_ROOT: /Users/peterroome/Sites/Everycare/Website/everycare/public/../config/..
Application Trace | Framework Trace | Full Trace

#{RAILS_ROOT}/app/views/newsitems/edit.rhtml:25:in `_run_rhtml_newsitems_edit'
#{RAILS_ROOT}/app/controllers/newsitems_controller.rb:49:in `update'

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:314:in `compile_and_render_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:290:in `render_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:249:in `render_file'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:699:in `render_file'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:621:in `render_with_no_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:742:in `render_without_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:691:in `render_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:643:in `render_with_no_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/layout.rb:240:in `render_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in `render'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in `render'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in `perform_action_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in `perform_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in `process_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in `process_without_session_management_support'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in `process'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in `dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:150:in `process_request'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:54:in `process!'
/usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/fcgi.rb:600:in `each_cgi'
/usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/fcgi.rb:597:in `each_cgi'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:53:in `process!'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:23:in `process!'
/Users/peterroome/Sites/Everycare/Website/everycare/public/dispatch.fcgi:24

#{RAILS_ROOT}/app/views/newsitems/edit.rhtml:25:in `_run_rhtml_newsitems_edit'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:314:in `compile_and_render_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:290:in `render_template'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:249:in `render_file'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:699:in `render_file'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:621:in `render_with_no_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:742:in `render_without_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:691:in `render_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:643:in `render_with_no_layout'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/layout.rb:240:in `render_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in `render'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in `render'
#{RAILS_ROOT}/app/controllers/newsitems_controller.rb:49:in `update'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in `perform_action_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in `perform_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in `process_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in `process_without_session_management_support'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in `process'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in `dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:150:in `process_request'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:54:in `process!'
/usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/fcgi.rb:600:in `each_cgi'
/usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.6.1/fcgi.rb:597:in `each_cgi'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:53:in `process!'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:23:in `process!'
/Users/peterroome/Sites/Everycare/Website/everycare/public/dispatch.fcgi:24

Request

Parameters: {"id"=>"1", "newsitem"=>{"topic_id"=>"4", "headline"=>"The new everycare website", "overview"=>"The new website is finally here thanks to Ceative Consulting.", "date"=>"2006-12-07", "id"=>"1", "content"=>" A new database driven webiste that has all the features of an expensive aplication. We would like to thnk them very muchf ro all they have done.\r\n "}}

Show session dump

---
flash: !map:ActionController::Flash::FlashHash {}


Response
Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}

Re: Very confusing error! Why?

Make sure to set the @topics instance variable in both the edit and update actions.

Railscasts - Free Ruby on Rails Screencasts