windowsアプリの経験はあるのですが、
今回、初めてwebアプリケーションを作成するので、webアプリの作法が分からず手間取っています。よろしくお願いします。
なんとかrailsにcocoonをインストールして追加ボタンで明細行を追加できる画面を作成しました。
しかし、1行目の名前列のcollection_selectの値はチーム列で選択した値で動的に変更させる事ができたのですが、明細行が複数になると2行目以降の名前列のcollection_selectが変更されません。
よろしくお願いします。
【画面のイメージ】
・画面ヘッダ部のtextboxは試合日程、対戦チーム等の情報を入力します
・追加ボタンをクリックするとチーム、名前を選択する明細行が追加される
・追加される明細は固定ではなく可変です
・明細行のチームを選択すると名前の選択内容を選択されたチームで絞り込む
【routes.rb】
get 'games/team_select' => 'games#team_select'
【コントローラー】
def team_select @player_parent = PlayerParent.where(team_id:params[:team_id]).pluck(:name, :id) @player_parent.unshift(["選択して下さい",""]) end
【Viewソース】
・追加ボタン部分(_form.html.erb)
<div class="form-group"> <h2>umpires</h2> <div class=""> <%= link_to_add_association "追加", f, :umpires, class: 'btn btn-default', data: { association_insertion_node: '#detail-association-insertion-point', association_insertion_method: 'append' }%> </div> <table class="table table-list"> <thead> <tr> <th><%= f.label :team_id, :class => 'control-label' %></th> <th><%= f.label :player_parent_id, :class => 'control-label' %></th> <th><%= f.label :position, :class => 'control-label' %></th> <th></th> </tr> </thead> <tbody id='detail-association-insertion-point'> <div class="form-group"> <%= f.fields_for :umpires do |tounament| %> <%= render 'umpire_fields',f: tounament %> <% end %> </div> </tbody> </table> </div>
・明細チーム選択部分(_umpire_fields.html.erb)
<tr class="nested-fields"> <%= f.hidden_field :id %> <td class="form-group"> <%= f.collection_select :team_id, @team, :id, :name,{:prompt => "選択してください"}, {:class => 'form-control ',:id => 'select_team'} %> </td> <td class="form-group"> <%= render 'player_parent_select',{:f => f} %> </td> ・・・・ <script type="text/javascript"> $(function($) { $('#select_team').change(function(){ var team_id = $("#select_team").val(); $.get("team_select.js?team_id=" + team_id); }); }); </script>
・明細選手選択部分(_player_parent_select.html.erb)
<%= f.collection_select :player_parent_id, @player_parent,:id,:name, {}, {:class => 'form-control',:id => 'games_player_parent'} %>
(team_select.js.erb)
$("#games_player_parent").html("<%= escape_javascript(options_for_select(@player_parent)) %>");
回答1件
あなたの回答
tips
プレビュー