Reputation: 351
i'm trying to add the model Posts to Blog. Post has blog_id. I need to set it when post create.
= link_to 'New Post', new_post_path(blog_id: @blog.id)
Here is the button to create new post. In address i see:
But I dont know how to send this blog_id to post_form. Should I use hidden form or what? Or maybe i need to write something in controller. Bear to me, i'm new at rails. Also, i want to know, is it the right way to set blog_id to post?
upd _post_form
= simple_form_for [@post] do |f|
= f.error_notification
.form-inputs
= f.input :title
= f.input :content
.form-actions
= f.button :submit
Upvotes: 0
Views: 55
Reputation: 23661
Just add a hidden_field
and access params[:blog_id]
which you are sending with new_post_path
= simple_form_for [@post] do |f|
= f.error_notification
.form-inputs
= f.input :title
= f.input :content
= f.hidden_field :blog_id, value: params[:blog_id]
.form-actions
= f.button :submit
Upvotes: 1
Reputation: 31
You should use relations between posts and blog, declare it in the posts model as has_many or has_one relationship so that will give you the chance to access to the object as:
@blog = blog.posts.find(:id)
i suggest you to follow this part of the tutorial: https://www.railstutorial.org/book/user_microposts it really helped my while doing a project
Upvotes: 1