Kitchen expected, got String

Hi All,
I have an object called kitchen that belong to another object
called house in a one to many relationship. For a new house, I collect
some info in the index page and then from the internal page and every
time I added it to session but when I got to the internalDetail action,
I’m getting Kitchen expected, got String exception. Not also that I
won’t be collecting the kitchen object actual attributes till the
internalDetail page (after that action). Also I have another object
inside house called (Bedroom) and that is not throwing an error.

def index

end

def internal
@house = House.new(params[:house])
session[:house] = @house
end

def internalDetail
#@house = params[:house]
@house = session[:house]
#@house.attributes = params[:house]
@house.update_attributes(params[:house])
session[:house] = @house
end

Any help is appreciated

Thanks

-Maged

You should be specifying kitchen_id, not kitchen.

On Fri, May 23, 2008 at 2:39 PM, Maged M. <
[email protected]> wrote:

end
Any help is appreciated

Thanks

-Maged

Posted via http://www.ruby-forum.com/.


Appreciated my help?
Recommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

Ryan B. wrote:

You should be specifying kitchen_id, not kitchen.

On Fri, May 23, 2008 at 2:39 PM, Maged M. <
[email protected]> wrote:

end
Any help is appreciated

Thanks

-Maged

Posted via http://www.ruby-forum.com/.


Appreciated my help?
Recommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

where to specify that?

Thanks
-Maged

Alex W. wrote:

Maged M. wrote:

Ryan B. wrote:

You should be specifying kitchen_id, not kitchen.

where to specify that?

Thanks
-Maged

I think what he means is that you form most likely is posting your
kitchen variable like this:

params[:house][:kitchen] #=> “123”

So when you do a:

House.new(params[:house])

Then when rails iterates through your params, it calls this internally:

house.kitchen = “123”

So house.kitchen, is an association. When assigning something to it,
Rails expects a Kitchen object. But your giving it the string id of a
kitchen object. This is where your error comes from. If you change
your form around so that it posts like this instead:

params[:house][:kitchen_id] #=> “123”

Then rails internally calls:

house.kitchen_id = “123”

Which should hook things up just fine. Whenever you are dealing with
associations, always remember that if you are playing with the id of the
other object, then you need to directly access that database column that
holds the foriegn id. If you are directly assigning ruby model objects,
then you should can use the association methods.

Thanks for the response but one question. If my form look like this:

<% form_for :house, :url => {:action => :internalDetail, :id => @house}
do |form| %>

in the page that go to the internalDetail action. how would I set the
kitchen_id or where I get it from in order to do
params[:house][:kitchen_id] #=> “123” in the form.

Sorry for newbie question

Thanks

Maged

Maged M. wrote:

Alex W. wrote:

Thanks for the response but one question. If my form look like this:

<% form_for :house, :url => {:action => :internalDetail, :id => @house}
do |form| %>

in the page that go to the internalDetail action. how would I set the
kitchen_id or where I get it from in order to do
params[:house][:kitchen_id] #=> “123” in the form.

Sorry for newbie question

Thanks

Maged

Thats not the important part. Where is the form attribute that you
select the kitchen_id with? Is it a select tag defined like:

<%= form.select :kitchen, … %>

That probably needs to be:

<%= form.select :kitchen_id, … %>

Maged M. wrote:

Ryan B. wrote:

You should be specifying kitchen_id, not kitchen.

where to specify that?

Thanks
-Maged

I think what he means is that you form most likely is posting your
kitchen variable like this:

params[:house][:kitchen] #=> “123”

So when you do a:

House.new(params[:house])

Then when rails iterates through your params, it calls this internally:

house.kitchen = “123”

So house.kitchen, is an association. When assigning something to it,
Rails expects a Kitchen object. But your giving it the string id of a
kitchen object. This is where your error comes from. If you change
your form around so that it posts like this instead:

params[:house][:kitchen_id] #=> “123”

Then rails internally calls:

house.kitchen_id = “123”

Which should hook things up just fine. Whenever you are dealing with
associations, always remember that if you are playing with the id of the
other object, then you need to directly access that database column that
holds the foriegn id. If you are directly assigning ruby model objects,
then you should can use the association methods.

Alex W. wrote:

Maged M. wrote:

Alex W. wrote:

Thanks for the response but one question. If my form look like this:

<% form_for :house, :url => {:action => :internalDetail, :id => @house}
do |form| %>

in the page that go to the internalDetail action. how would I set the
kitchen_id or where I get it from in order to do
params[:house][:kitchen_id] #=> “123” in the form.

Sorry for newbie question

Thanks

Maged

Thats not the important part. Where is the form attribute that you
select the kitchen_id with? Is it a select tag defined like:

<%= form.select :kitchen, … %>

That probably needs to be:

<%= form.select :kitchen_id, … %>

Here is my form that has the kitchen object

<%fields_for :kitchens do |kitchen| %>

so How to do that?

Kitchen Size: <%= kitchen.text_field :size , :size => 30 %>