S
S
Sworg2015-02-14 16:19:56
Ruby on Rails
Sworg, 2015-02-14 16:19:56

How to pass an object to a controller action?

There is a post controller and a comment controller.
Each post has a lot of comments, respectively.
In the index view of the post controller, along with each post, there is a form for adding a comment to a specific post.
I made a helper that generates an empty comment object (post.comments.build) to pass it to the form.
But how can I make it so that in the create action of the comment controller I create comments on a specific post?
After all, we cannot search for a post by id, for example, like this: post = Post.find_by(id: params[:id]) , because no id is actually transferred anywhere.
Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Vsk, 2015-02-15
@Sworg

You made a crutch with hidden field
What would be normal, you need to look at what nested resources routing is

T
timoo, 2015-02-14
@tomcat1911

Here 4.3 paragraph read carefully.
Briefly, then

@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params) #strong params

more or less like this.
As far as we know, the post id will be passed as params[:post_id].

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question