Welcome Guest | Login

undefined method 'to_sym' for nil:NilClass error ???

This is something I haven't seen before and haven't found much help on Google. I have a Contact model that allows people to sign up for a mailing list. Everything was working fine until last night when logged in as an admin and trying view the index action of contacts I received the error shown below. The problem only seems to occur when I try to link to the show action of the contact. If I take the link_to action away it works fine. I've checked my routes, contacts_controller, contact model, and relevant views but everything seems to be in order.

# production log error:

ActionView::TemplateError (undefined method `to_sym' for nil:NilClass) on line #5 of contacts/_contact.html.erb:
2:  <div class='contactName'>
3:   <p>
4:    <strong>
5:     <%= link_to contact.full_name_reversed, contact_path(contact) %>
6:    </strong>
7:    <br/>
8:   <%= mail_to contact.email, contact.email %>  

   vendor/rails/activesupport/lib/active_support/core_ext/hash/keys.rb:27:in `symbolize_keys'
   vendor/rails/activerecord/lib/active_record/attribute_methods.rb:160:in `inject'
   vendor/rails/activesupport/lib/active_support/core_ext/hash/keys.rb:26:in `each'
   vendor/rails/activesupport/lib/active_support/core_ext/hash/keys.rb:26:in `inject'
   vendor/rails/activesupport/lib/active_support/core_ext/hash/keys.rb:26:in `symbolize_keys'
   vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:69:in `url_for'
   (eval):17:in `contact_path'
   app/views/contacts/_contact.html.erb:5:in `_run_erb_47app47views47contacts47_contact46html46erb'
   vendor/rails/actionpack/lib/action_view/base.rb:637:in `send'
   vendor/rails/actionpack/lib/action_view/base.rb:637:in `compile_and_render_template'
   vendor/rails/actionpack/lib/action_view/base.rb:365:in `render_template'
   vendor/rails/actionpack/lib/action_view/base.rb:316:in `render_file'
   vendor/rails/actionpack/lib/action_view/base.rb:331:in `render'
   vendor/rails/actionpack/lib/action_view/partials.rb:120:in `render_partial'
   vendor/rails/actionpack/lib/action_view/partials.rb:143:in `render_partial_collection'
   vendor/rails/actionpack/lib/action_view/base.rb:602:in `each_with_index'
   vendor/rails/actionpack/lib/action_view/partials.rb:141:in `each'
   vendor/rails/actionpack/lib/action_view/partials.rb:141:in `each_with_index'
   vendor/rails/actionpack/lib/action_view/partials.rb:141:in `render_partial_collection'
   vendor/rails/actionpack/lib/action_view/base.rb:350:in `render'
   app/views/contacts/index.html.erb:2:in `_run_erb_47app47views47contacts47index46html46erb'
   vendor/rails/actionpack/lib/action_view/base.rb:637:in `send'
   vendor/rails/actionpack/lib/action_view/base.rb:637:in `compile_and_render_template'
   vendor/rails/actionpack/lib/action_view/base.rb:365:in `render_template'
   vendor/rails/actionpack/lib/action_view/base.rb:316:in `render_file'
   vendor/rails/actionpack/lib/action_controller/base.rb:1100:in `render_for_file'
   vendor/rails/actionpack/lib/action_controller/base.rb:836:in `render_with_no_layout'
   vendor/rails/actionpack/lib/action_controller/layout.rb:262:in `render_without_benchmark'
   vendor/rails/actionpack/lib/action_controller/benchmarking.rb:51:in `render'
   /usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
   vendor/rails/actionpack/lib/action_controller/benchmarking.rb:51:in `render'
   vendor/rails/actionpack/lib/action_controller/base.rb:1153:in `default_render'
   vendor/rails/actionpack/lib/action_controller/base.rb:1159:in `perform_action_without_filters'
   vendor/rails/actionpack/lib/action_controller/filters.rb:697:in `call_filters'
   vendor/rails/actionpack/lib/action_controller/filters.rb:689:in `perform_action_without_benchmark'
   vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
   /usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
   vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
   vendor/rails/actionpack/lib/action_controller/rescue.rb:199:in `perform_action_without_caching'
   vendor/rails/actionpack/lib/action_controller/caching.rb:678:in `perform_action'
   vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache'
   vendor/rails/activerecord/lib/active_record/query_cache.rb:8:in `cache'
   vendor/rails/actionpack/lib/action_controller/caching.rb:677:in `perform_action'
   vendor/rails/actionpack/lib/action_controller/base.rb:524:in `send'
   vendor/rails/actionpack/lib/action_controller/base.rb:524:in `process_without_filters'
   vendor/rails/actionpack/lib/action_controller/filters.rb:685:in `process_without_session_management_support'
   vendor/rails/actionpack/lib/action_controller/session_management.rb:123:in `process'
   vendor/rails/actionpack/lib/action_controller/base.rb:388:in `process'
   vendor/rails/actionpack/lib/action_controller/dispatcher.rb:171:in `handle_request'
   vendor/rails/actionpack/lib/action_controller/dispatcher.rb:115:in `dispatch'
   vendor/rails/actionpack/lib/action_controller/dispatcher.rb:126:in `dispatch_cgi'
   vendor/rails/actionpack/lib/action_controller/dispatcher.rb:9:in `dispatch'
   vendor/rails/railties/lib/fcgi_handler.rb:101:in `process_request'
   vendor/rails/railties/lib/fcgi_handler.rb:149:in `with_signal_handler'
   vendor/rails/railties/lib/fcgi_handler.rb:99:in `process_request'
   vendor/rails/railties/lib/fcgi_handler.rb:77:in `process_each_request'
   /usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.7/lib/fcgi.rb:612:in `each_cgi'
   /usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.7/lib/fcgi.rb:609:in `each'
   /usr/local/lib/ruby/gems/1.8/gems/fcgi-0.8.7/lib/fcgi.rb:609:in `each_cgi'
   vendor/rails/railties/lib/fcgi_handler.rb:76:in `process_each_request'
   vendor/rails/railties/lib/fcgi_handler.rb:50:in `process!'
   vendor/rails/railties/lib/fcgi_handler.rb:24:in `process!'
   dispatch.fcgi:24

# views/contacts/index.html.erb
<div id='contacts'>
 <%= render :partial => 'contact', :collection => @contacts%>
</div>


<div id='pagnination'>
<%= will_paginate @contacts %>
</div>

# views/contacts/_contact.html.erb
<div class="contact">
<div class='contactName'>
 <p>
  <strong>
   <%= link_to contact.full_name_reversed, contact_path(contact) %>
  </strong>
  <br/>
 <%= mail_to contact.email, contact.email %>  
 <span class="meta">Keep me updated:
  <span class='metaBig'>
  <% if contact.update_me? %>
   Yes Please
  <% else %>
   No Thanks
  <% end -%>
  </span>
  </span>
 </p>
</div>
</div>

# controllers/contacts_controller.rb (only index and show actions displayed here)
 def index
   @contacts = Contact.paginate  :page => params[:page], :per_page => 40,
                                 :order => 'last_name ASC'
 end

 def show
   @contact = Contact.find(params[:id])
# models/contact.rb

class Contact < ActiveRecord::Base
 ...validations...

 def full_name_reversed
   "#{last_name}, #{first_name}"
 end
 ...more...

end

2008-02-29 11:30 AM

Yeah this undefined method `to_sym' error showing up for link_to methods is a problem that is popping up more and more these days.  We're still not certain if it is a bug or needs a tweak that we're not familiar with -- for now you can probably just change the code to use

'/contact/' + contact.id.to_s  

instead of

contact_path(contact)

2008-02-29 01:56 PM

That fixed it! I had to make one small change to make it work for me.

I changed

'/contact/' + contact.id.to_s  

to

'/contacts/' + contact.id.to_s

But it didn't end there. I had to modify my edit and destroy links in a similar fashion and modify my edit form. Below are my original and revised link method structure and revised form in hopes that it can help any other unfortunate coder

# orignal edit link
<%= link_to 'Edit Contact', edit_contact_path(@contact) %>

# revised edit link
<%= link_to 'Edit Contact', '/contacts/' + @contact.id.to_s + '/edit' %>

# original destroy link
<%= link_to image_tag("trash.gif"), contact_path(@contact), :method => 'delete',
      :confirm => 'Are you sure you wish to destroy this contact? This cannot be undone.' %>

# revised destroy link
<%= link_to image_tag("trash.gif"), '/contacts/' + @contact.id.to_s, :method => 'delete',
        :confirm => 'Are you sure you wish to destroy this contact? This cannot be undone.' %>

# original edit form tag
<% form_for :contact, :url => contact_url(@contact), :html => { :method => :put } do |f| -%>

# revised edit form tag
<% form_for :contact, :url => '/contacts/' + @contact.id.to_s, :html => { :method => :put } do |f| -%>

Thanks for the help

~Josh

2008-02-29 02:43 PM


Hello Guest! In order to post you must be an active client with us, please log in or sign up today!