Parent child relations

I know this is a silly question, but I am just wondering when making the
link between children and the parent table, for example a blog reply, is
there a “best practice” way of doing this.

The only way that I can think of is to create a hidden input field
within
ex, (of course do it the
ruby way).

Is this the only way?

Thanks for the help

<% form_for :url => {:action => “create_reply”} %>
<%= form.hidden_field :post_id %>
<%= form.text_field :title, :size => 30 %>
<%= form.text_area :content, :cols => 40, :rows => 5 %.>
<%= submit_tag “Post”, :class => “submit” %>
<% end %>

Normally, one would use:

<% form_for :url => {:action => “create_reply”, :id => @posts} %>

This generates something like:

So, the actual post id is buried in the url to which you will issue your
POST request.

–steve

Keith L.-2 wrote:


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


View this message in context:
http://www.nabble.com/-Rails--Parent-child-relations-tf3140275.html#a8703916
Sent from the RubyOnRails Users mailing list archive at Nabble.com.

Thanks for the reply. I thought that doing it that way embeds the :id
value into the params hash table through the id key ex, params[:id]

Do you then have make the following setting manually?
params[:post_id] = params[:id]

Steve R. wrote:

Normally, one would use:

<% form_for :url => {:action => “create_reply”, :id => @posts} %>

This generates something like:

So, the actual post id is buried in the url to which you will issue your
POST request.

two ways to do what u want to do:

a) hidden field. you suggested this above.

b) via the controller when passing the id in :id => x ; but instead of

params[:post_id] = params[:id]
do
@reply.post_id = params[:id].to_i # the to_i isn’t really that
neccessary

That make sense. I guess I thought rails would have it’s own way of
doing this.

The only other thing that I am wondering about is whether the
params[:id] value must be cleared out to prevent the save method from
thinking that the attempted insert is actually an update. I will have
to check this out after work.

Thanks for all the help.

<% form_for :reply, :url => {:action => ‘create_reply’} do |form| %>

<% end %>

////
def create_reply
params[:post_id] = params[:id]
params[:id] = nil

post = Post.new(params[:reply])
post.save
end