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
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:
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.
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:
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.
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| %>
Kitchen Size:
<%= kitchen.text_field :size , :size => 30 %>
so How to do that?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.