Associations with value passing

22 views
Skip to first unread message

Szabolcs Toth

unread,
Feb 15, 2016, 12:28:47 PM2/15/16
to rubyonra...@googlegroups.com
I have completely lost with this simple problem. I have two model, Post
and Comment. I added the one-to_many associations. When I want to add a
comment to a certain post I use this:

<%= link_to 'Add comment', new_comment_path(id: post.id) %>

In the comments_controller.rb I have
def create
@comment = Comment.new(comment_params)
@comment.post_id = params[:id]

But it doesn't work, what did I miss? Thank you!

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

Walter Lee Davis

unread,
Feb 15, 2016, 5:37:09 PM2/15/16
to rubyonra...@googlegroups.com
If you're sending this to the comments controller, then set the post_id attribute in that form. id always refers to the object of the form (in this case the comment) rather than the parent.

Walter

> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b8675d6aca1ce38b6be0059004caf2b8%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Szabolcs Toth

unread,
Feb 16, 2016, 7:48:17 AM2/16/16
to rubyonra...@googlegroups.com
Walter Davis wrote in post #1181415:
>> @comment = Comment.new(comment_params)
>> @comment.post_id = params[:id]
>>
>> But it doesn't work, what did I miss? Thank you!
>>
>
> If you're sending this to the comments controller, then set the post_id
> attribute in that form. id always refers to the object of the form (in
> this case the comment) rather than the parent.
>
> Walter

Thanks, last night I tried this in the _form partial:
<%= f.text_field :post_id, value: params[:id] %>

This way it is work, but I wouldn't like to show this to user, I'd like
to do it automatically. How can I do that?

Walter Lee Davis

unread,
Feb 16, 2016, 8:12:11 AM2/16/16
to rubyonra...@googlegroups.com
It's a matter of personal taste *where* you stash this attribute, but you will need to do this no matter how you handle the form submission. The comments controller has no particular notion of which post it is commenting on otherwise.

* You can put the post_id in the querystring when you link to the comment form, and capture it with a hidden variable in your new comment form.
* You can use nested routes to do the same thing in the body of the URL: posts/1234/comments/new
* I suppose you could stash it in a session cookie if you really object to the visual noise
* You could try using a nested form, but that gets hairy quite quickly if you have a public comment form (displayed on the posts/show route) and private edit both going through the posts/update route -- where do you redirect after, and where do you go on error during update?

The simplest possible thing, from way back to the 15-minute blog demo (whoops!) is to carry the post_id into the comments/new form in a querystring. It's not shameful or anything.

Walter

Rob Lane

unread,
Feb 16, 2016, 2:25:47 PM2/16/16
to Ruby on Rails: Talk
If you just want to hide the field use:
<%= f.hidden_field :post_id, value: params[:id] %>

Reply all
Reply to author
Forward
0 new messages