我如何創建一個鏈接,在我的歌曲模型中添加「添加更多字段」?我能夠爲我的事件has_many歌曲關係嵌套屬性。我是否需要爲歌曲做同樣的事情?嵌套屬性Rails
這裏是歌頁,與新的歌曲創作處理,同時選擇哪個事件將歌曲添加到:
<br>
<br>
<div class ="container">
<div class="jumbotron">
<h2> Select an event to add songs to: </h2>
<%= form_for @song do |f| %>
<%= f.collection_select(:event_id, Event.all, :id, :name) %>
<h3> Enter your song </h3>
<%= f.text_field :artist, placeholder: "Artist" %>
<%= f.text_field :title, placeholder: "Title" %>
<%= f.text_field :genre, placeholder: "Genre" %>
<h2> Enter the partycode for that event: </h2>
<%= form_for Event.new do |f| %>
<%= f.text_field :partycode, :required => 'required'%>
<%= f.submit "Submit", class: "btn btn-primary", :onclick => "window.alert('Aye');" %>
<% end %>
<h2><%= link_to_add_fields "Want to add more songs?", f, :songs %> </h2>
<% end %>
</div>
</div>
的link_to_add_fields在這裏被定義在ApplicationHelper文件:
module ApplicationHelper
def link_to_add_fields(name, f, association)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render("songs_fields", f: builder)
end
link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
end
end
我在這裏得到一個錯誤,說在這個ApplicationHelper文件中說:
您不能在另一個form_for中使用一個'form_for'。 –