0

私は次のことをしようとしています: イベントでは、個々の学生が競争することも、学生のチームが競争することもできます。そこで、次のようにポリモーフィックに設定しました。

class Event < ActiveRecord::Base
  belongs_to :event_type
  has_many :competitors
  has_many :students, through: :competitors, :source => :participant, :source_type => 'Student'
end

class Competitor < ActiveRecord::Base
  belongs_to :event
  belongs_to :participant, polymorphic: true

  # also has other attributes, like points, and grade
end

class Student < ActiveRecord::Base
  has_many :competitors, :as => :participant
  has_many :events, :through => :competitors
end

class Team < ActiveRecord::Base
  has_many :competitors, :as => :participant
  has_many :events, :through => :competitors
  has_many :team_members
  has_many :students, :through => :team_members
end

tl;dr: Resourceful コントローラーを使用してこれらを操作するにはどうすればよいですか?

プロパティをCRUDできるイベントコントローラーが既にあります。同じことを行う学生コントローラーがあります。そのため、それらを組み合わせるには競合他社コントローラーが必要です。そして、私が空白を描くのはそこです。

私が望むのは、学生のチェックボックスのリストから、1 つの画面でイベントの競合他社を選択できるようにすることです。次のようなネストされたルートを作成すると

resources :events, shallow: true do
  resources :competitors, shallow: true
end

次にevents/1/competitors、競合他社のリストを表示します。しかし、それでは、チームまたは学生を表示する必要がありますか? if/else の混乱になりたくない。では、これら 2 つのコントローラーを別々に用意する必要がありますか? 彼らはどのように名付けられますか?そして、私は彼らとどのように交流しますか?

学生の競技者専用のコントローラーを作成したとしましょう。

resources :events, shallow: true do
  resources :student_competitors, shallow: true
end

私は得るだろうevents/1/student_competitors. これにより、そのイベントに参加しているすべての学生のリストが表示されます。そのリストを更新ボタン付きのチェックボックスのセットにしたいとしましょう。そのフォームをどのように構築しますか?

<%= simple_form_for(@event, :html => { :class => 'form-vertical' }) do |f| %>
  <% @students.each do |student| %>
    <%= check_box_tag "event[student_ids][]", student.id, student.events.include?(@event) %>
    <span><%= student.name %></span>
  <% end %>

  <%= f.button :submit, 'Update Competitors' %>
<% end %>

これはフォームを に送信events/1しますが、StudentCompetitors コントローラーに送信したいのですが、simple_form_forのようなコレクションで呼び出しsimple_form_for(@event, @competitors)て更新アクションへのパスを構築させることはできません。

本質的に、これらすべてについて最善の方法がわかりません。リソースに固執して、厳しすぎますか? あなたの助けに感謝します。

4

1 に答える 1