Rails Form Association Automatically New -
i have rails form movies. in form title , trailer_id (which has youtube url)
trailer_id association trailers scaffold.
so when go /trailers/new , add new youtube url, can see in dropdown on movies/new.
but how automatically create new trailer when entered in movies form
so instead of having me go trailers/new , create new trailer how create directly movies form
you can use nested_form gem.
add gemfile
gem "nested_form"
run bundle
add application.js
//= require jquery_nested_form
add in movie.rb
has_many :trailers accepts_nested_attributes_for :trailers
and in form view:
<%= nested_form_for @movie |f| %> <%= f.fields_for :trailers |trailer_form| %> <%= trailer_form.text_field :link %> <%= trailer_form.link_to_remove "remove trailer" %> <% end %> <p><%= f.link_to_add "add trailer", :trailers %></p> <% end %>
you can see more details on https://github.com/ryanb/nested_form
Comments
Post a Comment