[Rails] Confused...

Jeff Wood
Thu, 05 Jan 2006 23:17:04 -0000

Ah, more info ...

Ok, so, I have a button on the parent detail page ... to take me to
entry for a new child.  I''m passing the parents id as a parameter
using

<%= button_to :action => ''new'', :controller =>
''child'', :parent_id =>
@parent[] %>

So, the url to the new page is showing correctly:

http://hostname:port/child/new?parent_id=1

... so, in the new method for the child_controller I do

@child = Child.new()
@parent = Parent.find( params[ :parent_id ] )
@child.parent = @parent

... that seems to work.

so, that forms follows normal scaffolding and calls create on form submit.

I don''t understand how the attributes in create get set ... and, what
value I should be looking for when trying to save the association from
Parent to Child.

Right now, that attribute just gets ignored and the record gets saved
in the database with a null value for the parent id (in the child
table).

... that''s where I''m stuck.

Thanks for your responses (past & future) & your time.

j.

On 1/5/06, Mike Harris <GENIE@prodigy.net> wrote:
> It would be helpful to give more information about how it''s not
working.
>
> Jeff Wood wrote:
>
> >Ok,
> >
> >So, I''ve got two data tables associated through a foreign key.
> >
> >class Child < ActiveRecord::Base
> >  belongs_to :parent, :class => Parent, :foreign_key =>
"parent_id"
> >  # also has attr1, attr2
> >end
> >
> >class Parent < ActiveRecord::Base
> >  has_many :children, :class => Child, :foreign_key =>
"parent_id"
> >end
> >
> >So, I''m trying to build the new/edit form for the child
table...
> >
> >Basically, I was aiming for something like:
> >
> ><%= text_field "child", "id", :locked =>
"true" %>
> ><%= text_field "child.parent", "id", :locked
=> "true" %>
> ><%= text_field "child", "attr1" %>
> ><%= text_field "child", "attr2" %>
> >
> >
> >but, that isn''t working ... Can anybody enlighten me ?
> >
> >j.
> >
> >--
> >"Remember. Understand. Believe. Yield! ->
http://ruby-lang.org"
> >
> >Jeff Wood
> >_______________________________________________
> >Rails mailing list
> >Rails@lists.rubyonrails.org
> >http://lists.rubyonrails.org/mailman/listinfo/rails
> >
> >
> >
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"

Jeff Wood