【问题标题】:Rails Nested Model FormRails 嵌套模型表单
【发布时间】:2012-07-10 11:47:08
【问题描述】:

目前我有三个模型:

Songs have many Setlists through Allocations
Setlists have many Songs through Allocations
Allocations belong to Setlists and belong to Songs

我现在的表格是这样的:

 <%=f.label :date, "Set a date" %>
 <%=f.date_select :date%>

 <div>
  <%= render 'song' %> 
 </div>

 <%=f.submit "Create Setlist", class: "btn btn-large btn-primary" %>

上面给出的部分歌曲是:

<div id="songSelection">
<table class= "table table-striped table-bordered">
        <thead>
            <th>Title</th>
            <th>Artist</th>
            <th>Add to Set</th>
        </thead>
            <tbody>
      <% @songs.each do |song| %>
        <tr>
           <%= nested_form_for(@setlist.allocations.build(song_id: song.id)) do |builder| %>
           <td><%= song.title %></td>
           <td><%= song.artist %></td>
           <td>
              <%= builder.submit "Add Song", class: "btn btn-small btn-primary" %>
            <% end %>
           </td>
        </tr>
      <% end %>
    </tbody>
</table>
</div>

基本上,我正在尝试通过分配将歌曲分配到设置列表。目前它指出歌曲部分中的 f 没有定义。如果我摆脱它,则页面呈现正常但无法正常运行(即,每当我单击按钮添加歌曲时,它会重定向它会将更改保存到设置列表日期但不分配任何新分配)。任何帮助将非常感激。我意识到我可能错过了一些信息,因为我对此有些陌生,所以如果需要任何其他详细信息,请询问。

我尝试使用 Ryan Bates 的嵌套表单 gem,但无法正常工作。我认为这是因为它在尝试构建它时没有为分配分配歌曲 ID,但不确定我将它放在哪里/如何放置。

【问题讨论】:

    标签: ruby-on-rails ruby form-for


    【解决方案1】:

    【讨论】:

    • 我已经尝试过使用它,但我仍然遇到问题...这是我的代码:&lt;tbody&gt; &lt;% @songs.each do |song| %&gt; &lt;tr&gt; &lt;%= nested_form_for(@setlist.allocations.build(song_id: song.id)) do |builder| %&gt; &lt;td&gt;&lt;%= song.title %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= song.artist %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= song.key %&gt;&lt;/td&gt; &lt;td&gt; &lt;%= builder.submit "Add Song", class: "btn btn-small btn-primary" %&gt; &lt;% end %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;/tbody&gt;
    • 哎呀,根本没有格式化!将其发布在实际问题中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多