DEV Community

n350071๐Ÿ‡ฏ๐Ÿ‡ต
n350071๐Ÿ‡ฏ๐Ÿ‡ต

Posted on

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

Top comments (0)