Forum: Ruby on Rails Confused...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jeff W. (Guest)
on 2006-01-06 00:33
(Received via mailing list)
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" %><br/>
<%= text_field "child.parent", "id", :locked => "true" %><br/>
<%= text_field "child", "attr1" %><br/>
<%= text_field "child", "attr2" %><br/>


but, that isn't working ... Can anybody enlighten me ?

j.

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

Jeff W.
Mike H. (Guest)
on 2006-01-06 00:48
(Received via mailing list)
It would be helpful to give more information about how it's not working.
Jeff W. (Guest)
on 2006-01-06 03:19
(Received via mailing list)
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 H. <removed_email_address@domain.invalid> wrote:
> >  # also has attr1, attr2
> ><%= text_field "child", "id", :locked => "true" %><br/>
> >"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


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

Jeff W.
This topic is locked and can not be replied to.