Forum: Ruby on Rails Couldn't find <Class> without an ID

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
2e1af8b6323bf2c6825c4166b389b3ad?d=identicon&s=25 Mike Rood (imdwalrus)
on 2007-02-15 08:24
(Received via mailing list)
I'm just starting to use forms and I'm stuck.  I'm working with two

class Department < ActiveRecord::Base
  has_many   :employees

class Employee < ActiveRecord::Base
  belongs_to :department

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])
      flash[:notice] = 'Department was successfully created.'
      redirect_to :action => 'list'
      render :action => 'new'

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

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

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_for :department, :url => { :action => @form_action } do |form|
    <label for="department_name">Name:</label>
    <%= form.text_field :name  %>
    <label for="department_manager_id">Manager:</label>
    <%= :manager_id, @managers %>
    <span class="submit"><%= submit_tag set_submit_tag_text(
<% end %>

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

Thanks for taking the time to look at this.

-- Mike
526d58efe4c2a0883880c8106b386cf4?d=identicon&s=25 rschlimmer (Guest)
on 2007-02-15 10:45
(Received via mailing list)
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
2e1af8b6323bf2c6825c4166b389b3ad?d=identicon&s=25 Mike Rood (imdwalrus)
on 2007-02-15 16:55
(Received via mailing list)
Hi Ryan,

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

-- Mike
This topic is locked and can not be replied to.