Damir Nurgaliev Damir Nurgaliev - 1 month ago 5
Ruby Question

How to read information from request data to controller

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:


http://localhost:3000/posts/new?blog_id=27


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

Answer

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