DEV Community

n350071πŸ‡―πŸ‡΅
n350071πŸ‡―πŸ‡΅

Posted on

3 1

Making a nested params for xxx_tag of View Helper in Rails

πŸ€” Situation

Want to make like this params that have Hash inside an Array at the award_content.
​

[1] pry> params
=> {"utf8"=>"βœ“",
 "books"=>
  {"author_name"=>"Minato",
   "book_name"=>"HELLO",
   "category"=>"Fiction",
   "awards"=>[{"award_id"=>"1", "award_title"=>"new", "award_content"=>"award for new people"},
              {"award_id"=>"2", "award_title"=>"movie", "award_content"=>"it becames to movie"}
             ]},
 "action"=>"create",
 "controller"=>"books"}

​

πŸ¦„ How to do it?

πŸ‘ in erb

<%= hidden_field_tag 'books[awards][][award_id]', award.id %>
<%= hidden_field_tag 'books[awards][][award_title]', award.title %>
<%= hidden_field_tag 'books[awards][][award_content]', award.content %>

​

πŸ‘ in model

Turn accept nested attribuetes.
​

class Author
  has_many :awards
  accepts_nested_attributes_for :awards
  validates_precence_of :awards
end

​


πŸ”— Parent Note

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’