前提・実現したいこと
userとteamを多対多で実装しています。
teamの編集等をcurrent_userがteam_userである場合のみ許可したいです。
発生している問題
teams_contoroller.rbにbefore_actionで下記のように記述しても
current_userが1なのに対しteam.usersが複数なので機能しません。
before_action :correct_team_user, except: [:new, :create] private def correct_team_user @team = Team.find(params[:id]) redirect_to root_path unless current_user == @team.users end
team.rb class Team < ApplicationRecord has_many :team_users has_many :users, through: :team_users end
team_user.rb class TeamUser < ApplicationRecord belongs_to :team belongs_to :user end
user.rb class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :team_users has_many :teams, through: :team_users end
試したこと
current_user == @team.user ではエラーになります。
TeamとUserの定義も書きましょう。
current_user(単数) と @team.users(複数)を等号で比較するのはナンセンスなのでは…。
回答1件
あなたの回答
tips
プレビュー