0
#The various models
class Team < ActiveRecord::Base
has_many :competition_teams
has_many :competitions, through: :competition_teams
end
class Competition < ActiveRecord::Base
has_many :competition_teams
has_many :teams, through: :competition_teams
end
class CompetitionTeam < ActiveRecord::Base
belongs_to :team
belongs_to :competition
end
#The form I'm using to add teams to the competition
= semantic_form_for @competition do |f|
= f.inputs :name do
= f.input :teams, as: :select, collection: current_user.teams.select{|t| [email protected]?(t)}
= f.actions do
= f.action :submit, as: :button
#Competition update action, used to add teams
def update
@competition = Competition.find(params[:id])
teams = competition_params[:team_ids] + @competition.teams.pluck(:id)
team = Team.find(competition_params[:team_ids][1])
if team.users.pluck(:id).include?(current_user.id) && @competition.update_attribute(:team_ids, teams)
redirect_to @competition
end
end
所以我想要做的是創建一個按鈕(或鏈接),允許用戶從競爭對手中刪除他們的團隊。這應該通過自定義操作還是某種形式來完成? 我真的不知道在哪裏可以從這裏走,所以任何幫助是非常讚賞以多對多的關係刪除/刪除數據
所以我應該爲competition_teams創建一個控制器,它只能用於一個目的,刪除CompetitionTeams。我想避免這種情況,但它使一切變得更加容易。我可以在比賽控制器中使用摧毀動作來做同樣的事情,但我想最好是這樣做。非常感謝你! 獲得@team對象也很難,因爲我沒有team_id來獲取它。我查看並希望編輯的頁面是'比賽/:id'。 – norflow
在比賽控制器中創建它是沒有意義的,因爲那是您創建銷燬行爲來摧毀競爭對手的地方。這將會讓人困惑。 – forthowin