0

I have a rails form for movies. In this form is Title and Trailer_id (which has the youtube url)

Trailer_id is an association with the trailers scaffold.

So when I go to /trailers/new and add a new youtube url, I can then see it in a dropdown on my movies/new.

But how do I automatically create a new trailer when entered in the movies form

So instead of having me go to trailers/new and create a new trailer How do I create it directly from the Movies form

4

1 に答える 1

0

nested_form ジェムを使用できます。

これをあなたのgemfile

gem "nested_form"

走るbundle

これを application.js に追加します

//= require jquery_nested_form

これを追加movie.rb

has_many :trailers
accepts_nested_attributes_for :trailers

そしてあなたのフォームビューで:

<%= nested_form_for @movie do |f| %>
  <%= f.fields_for :trailers do |trailer_form| %>
    <%= trailer_form.text_field :link %>
    <%= trailer_form.link_to_remove "Remove this trailer" %>
  <% end %>
  <p><%= f.link_to_add "Add a trailer", :trailers %></p>
<% end %>

詳細については、https://github.com/ryanb/nested_formを参照してください。

于 2013-05-14T23:47:52.257 に答える