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