randomguy
randomguy

Reputation: 12242

Noob Rails 3 link_to question

Lets say we have a Article with many Comments.

Comments are added into the article in a separate "create comment for article XYZ" - page.

I'd like to have a "add comment" link on every article page.

But in the comments controller, I need to have information about the parent article the comment is added to.

So how do I pass along the article information (article_id) through the link_to to the comments create page?

Upvotes: 1

Views: 483

Answers (1)

Monza
Monza

Reputation: 71

<%= link_to "add comment", new_comment_path(:article_id => article_id) %>

Then in your Comments controller, the "new" action:

@article_id = params[:article_id]

Then in the Views/Comments/new.html.erb you have access to the @article_id variable and are able to insert into the form with a hidden field and pass it to the create action...

Upvotes: 1

Related Questions