Couldn't find <Class> without an ID

I'm just starting to use forms and I'm stuck. I'm working with two models:

class Department < ActiveRecord::Base   has_many :employees end

class Employee < ActiveRecord::Base   belongs_to :department end

I can create records just fine. When I go back to edit them, I get the message Couldn't find <Class> without an ID, where <Class> is either Department or Employee.

My department controller contains these methods:

  def create     @department = Department.new(params[:department])     if @department.save       flash[:notice] = 'Department was successfully created.'       redirect_to :action => 'list'     else       render :action => 'new'     end   end

  def edit     @department = Department.find(params[:id])     @managers = Employee.find(:all,                                 :order => 'last_name, first_name',                                 :conditions => "manager = 1").map {| m>                                      [#{m.last_name}, #{m.first_name}" , m.id]}     render :layout => 'isis-form'   end

  def update     breakpoint     @department = Department.find(params[:id])     if @department.update_attributes(params[:department])       flash[:notice] = 'Department was successfully updated.'       redirect_to :action => 'show', :id => @department     else       render :action => 'edit'     end   end

The layout simply wraps the form in a <div>, which supplies some formatting, and a <fieldset>.

My form partial is:

<%= error_messages_for 'department' %>

<% @form_action = set_form_action(@params[:action]) %> <% logger.debug "params[action] = #{@params[:action]}" %> <% logger.debug "form_action in form = #{@form_action}" %> <% breakpoint %> <!--[form:department]--> <% form_for :department, :url => { :action => @form_action } do |form| %>   <p>     <label for="department_name">Name:</label>     <%= form.text_field :name %>   </p>   <p>     <label for="department_manager_id">Manager:</label>     <%= form.select :manager_id, @managers %>   </p>   <p>     <span class="submit"><%= submit_tag set_submit_tag_text(                                                    @params[:action]) %></span>   </p> <% end %> <!--[eoform:department]-->

The set_form_action and set_submit_tag_text methods just set the :action and button text depending on the action that calls the form. The logger messages show me that :action => @form_action is getting set to "update".

I can see from the breakpoints that params[:id] is set properly when I enter the from from the edit action and that it doesn't exist at all when I get to the update action. All the form fields are carried over properly, but :id isn't.

I have exactly the same problem with the employee form, which I guess isn't surprising since I coded it the same way.

I'm sure I've just done something really dumb, but after several hours I still can't see it and I can't find any references that seem to describe this problem. Can somebody please tell me what I'm doing wrong?

Thanks for taking the time to look at this.

-- Mike

I believe the solution is to pass the :id value in addition to the :action parameter for your form within the partials.

<% form_for :department, :url => { :action => @form_action, :id => params[:id] } do |form| %>

-- Ryan

Hi Ryan,

You nailed it! That worked like a champ. Thanks so much for taking the time to help me out.

-- Mike