###前提・実現したいこと
このリンクのように
フォームを動的に追加したい。
事情によりgemのnested_formなどは使えません。
###発生している問題・エラーメッセージ
###該当のソースコード
ruby
1module ApplicationHelper 2 def link_to_add_fields(name, f, association) 3 new_object = f.object.send(association).klass.new 4 id = new_object.object_id 5 fields = f.fields_for(association, new_object, child_index: id) do |builder| 6 render(association.to_s.singularize + "_fields", f: builder) 7 end 8 link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")}) 9 end 10end
ruby
1views/groups/_words_fields 2 3<fieldset> 4 <%= f.hidden_field :user_id, value:current_user.id %> 5 6 <%= f.label :title,"タイトル"%> 7 <%= f.text_field :title, class: 'form-control' %> 8 9 <%= f.label :question,"問題"%> 10 <%= f.text_area :question, class: 'form-control' %> 11 12 <%= f.label :answer, "回答" %> 13 <%= f.text_area :answer, class: 'form-control' %> 14 15 <% tag_key= "add_words_#{Time.now.to_i}" %> 16 17 <label>タグ<ul id="<%="#{tag_key}"%>"></ul></label> 18 19 <%=javascript_tag do%> 20 $('#<%="#{tag_key}" %>').tagit({ 21 fieldName: 'words[][tag_list]', 22 singleField: true 23 }); 24 <%end%> 25 <%= f.hidden_field :_destroy %> 26 <%= link_to "削除", '#', class: "remove_fields" %> 27 <hr> 28</fieldset>
ruby
1views/groups/new 2<div class="field"> 3<div class="row"> 4 <div class="col-md-6 col-md-offset-3"> 5 <%= form_for(@groups) do |f| %> 6 <%= render 'shared/error_messages', object: f.object %> 7 8 <%= f.hidden_field :user_id, value:current_user.id %> 9 10 <%= f.label :title,"問題集名"%> 11 <%= f.text_field :title, class: 'form-control' %> 12 13 <%= f.label :explanation,"説明"%> 14 <%= f.text_area :explanation, class: 'form-control'%> 15 16 <%= f.label :image,"ホーム画像" %> 17 <%= f.file_field :image,class: 'form_control'%> 18 <br> 19 <label>タグ<ul id="group"></ul></label> 20 21 <br> 22 <hr> 23 <%= f.fields_for :words do |builder| %> 24 <%= builder.hidden_field :user_id, value:current_user.id %> 25 26 <%= builder.label :title,"タイトル"%> 27 <%= builder.text_field :title, class: 'form-control' %> 28 29 <%= builder.label :question,"問題"%> 30 <%= builder.text_area :question, class: 'form-control' %> 31 32 <%= builder.label :answer, "回答" %> 33 <%= builder.text_area :answer, class: 'form-control' %> 34 <label>タグ<ul id="small"></ul></label> 35 <%= render "words_fields", f: builder%> 36 37 <%end%> 38 <hr> 39 <%= link_to_add_fields "住所追加", f,:words%> 40 <%= f.submit '完了', class: "btn btn-primary" %> 41 <% end %> 42 <br> 43 <%=link_to "再入力",new_group_path %> 44 </div> 45</div> 46</div> 47
###試したこと
先程のリンクのように書いてみました。
###補足情報(言語/FW/ツール等のバージョンなど)
ruby2.3.1
rails4.2.7
回答1件
あなたの回答
tips
プレビュー