# Couldn't find \<Class\> without an ID

**URL:** <https://www.ruby-forum.com/t/couldnt-find-class-without-an-id/85653>\
**Category:** Rails\
**Created:** [February 15, 2007, 8:24am UTC](https://www.ruby-forum.com/t/couldnt-find-class-without-an-id/85653 "2007-02-15T08:24:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [February 15, 2007, 8:24am UTC](https://www.ruby-forum.com/t/couldnt-find-class-without-an-id/85653/1 "2007-02-15T08:24:31Z")

</div>

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 without an ID, where 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

, which supplies some  
formatting, and a .

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|  
%\>

Name: \<%= form.text\_field :name %\>

Manager: \<%= form.select :manager\_id, @managers %\>

\<%= submit\_tag set\_submit\_tag\_text( @params[:action]) %\>

\<% 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  
wrong?

Thanks for taking the time to look at this.

– Mike

---

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [February 15, 2007, 10:45am UTC](https://www.ruby-forum.com/t/couldnt-find-class-without-an-id/85653/2 "2007-02-15T10:45:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [February 15, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/couldnt-find-class-without-an-id/85653/3 "2007-02-15T16:55:31Z")

</div>

Hi Ryan,

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

– Mike
