我想创建一个带有邀请系统的应用程序,用户可以在其中邀请成员加入不同的团队。但我无法让它工作。我有四个模型对象 - User
, Team
, Member (join table between User & Team)
& Invitation
- 这些看起来像这样:
#User
has_many :members
has_many :teams, through: :members
accepts_nested_attributes_for :teams
accepts_nested_attributes_for :members
has_many :sent_invitations, class_name: "Invitation", foreign_key: "sender_id"
has_many :invitations
#Team
has_many :members
has_many :users, through: :members
has_many :invitations
#Invitation
has_one :sender, class_name: "User"
has_one :recipient, class_name: "User"
belongs_to :team
我有一个ActionMailer
向用户发送邀请,每个Invitation
对象都有一个令牌。当我创建新用户时,我想以安全的方式将受邀用户添加到正确的团队中。我的用户控制器如下所示:
# GET /users/new
def new
@user = User.new
if params[:invitation_token]
@user.email = Invitation.find_by_token(params[:invitation_token]).recipient_email
@invitation = Invitation.where(token: params[:invitation_token])
else
@team = @user.teams.build
end
end
# POST /users
# POST /users.json
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
sign_in @user
# Private method for adding users to their teams.
add_to_team
[...]
def add_to_team
if @invitation
puts "Should add to team" # But never gets called!
@user.teams << @invitation.team
@user.invitations << @invitation
@user.save!
end
end
似乎我的 if 语句add_to_team
永远不会评估为真。出于某种原因,我@invitation
的为零,我不知道为什么。
有任何想法吗?
更新
尝试将我的@invitation
实例变量改为我的创建操作。像这样:
def create
@user = User.new(user_params)
if params[:invitation_token]
@invitation = Invitation.where(token: params[:invitation_token])
end
#And
def add_to_team
if @invitation
# Never gets called
@user.teams << @invitation.team
@user.invitations << @invitation
puts "Invited by team: #{@invitation.team.name}"
@user.save!
end
end
但是 if 语句中的代码仍然没有被调用。