Forum: Ruby on Rails Update action | NoMethodError | nil.to_sym

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
39025a7f572d6fe464e1fe6be399ca3b?d=identicon&s=25 Mike Dershowitz (mdersh53)
on 2008-10-25 20:33
(Received via mailing list)
Ok, totally no reason why I'm getting an error in this model update
and no other.  HELP!

CONTROLLER:

  # GET /employees/1/edit
  def edit
    @employee = Employees.find(params[:id])

  end

VIEW:

<h1>Edit Employee</h1>
<div id="main-sub">
<%= error_messages_for :employees %>

<% form_for(@employee) do |f| %>
<table width="600" border="0">
    <tr>
        <td class="label">Email:
        </td>
        <td><%= f.text_field :email %> (required, will be login to
intranet)
        </td>
    </tr>

ROUTES:

  map.resources :employees

All pretty standard stuff - what's going on?

ERROR:

 NoMethodError in Employees#edit

Showing employees/edit.html.erb where line #5 raised:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.to_sym

Extracted source (around line #5):

2: <div id="main-sub">
3: <%= error_messages_for :employees %>
4:
5: <% form_for(@employee) do |f| %>
6: <table width="600" border="0">
7:     <tr>
8:         <td class="label">Email:

Thanks so much in advance!
84b661cb6bd8a870c1787968ee945962?d=identicon&s=25 Patrick Sullivan (ywp)
on 2008-10-25 21:44
(Received via mailing list)
Look in your controller where you assign @employee. You are calling find
on a
plural model (Employees.find). Is your model defined as "Employees"? The
Rails convention is to have models named in a singular form, ie
Employee,
because it defines an instance of a singleton object.

Try changing Employees.find to Employee.find and see if that clears up
the
funkiness.

Also, this might be way off, but some attribute names can cause problems
with
ActiveRecord. Attributes that have the same name as internal
ActiveRecord
methods cause sporadic errors. The Rails wiki has a running list of
pseudo-reserved words known to cause problems with AR:
http://wiki.rubyonrails.org/rails/pages/ReservedWords
Fac81a9d95c81d817a2debb6f717f3ac?d=identicon&s=25 Sazima (Guest)
on 2008-10-26 13:06
(Received via mailing list)
Miked,

Basically your var @employee is nil when you pass it to form_for().
That means Employees.find(params[:id]) is returning nil (either is not
working, or the :id doesn't exist)...

As Patrick put, the model name is not standard (should be Employee)
and finding by hand may also help.

Cheers, Sazima
This topic is locked and can not be replied to.