【问题标题】:Rails Form Association Automatically NewRails 表单关联自动新建
【发布时间】:2013-05-14 22:36:36
【问题描述】:

我有一个用于电影的导轨表格。这种形式是 Title 和 Trailer_id(有 youtube url)

Trailer_id 是与拖车脚手架的关联。

所以当我转到 /trailers/new 并添加一个新的 youtube 网址时,我可以在我的电影/新的下拉列表中看到它。

但是在电影表单中输入时如何自动创建新预告片

所以不要让我去预告片/新建并创建一个新预告片 如何直接从电影表单创建它

【问题讨论】:

    标签: ruby-on-rails forms associations


    【解决方案1】:

    您可以使用nested_form gem。

    将此添加到您的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查看更多详情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多