无法让用户创建自定义消息,设计可邀请
问题描述:
我的应用中有2个类似用户的模型:'参与者'和'成员'。 我试图让他们在通过Devise Invitable邀请其他成员/参与者时包含自定义消息。但是,我无法让它工作。无法让用户创建自定义消息,设计可邀请
我正在关注这个official tutorial,所以我做了以下更改以覆盖Devise Invitable Controller,但是在使用pry时,似乎此自定义控制器在发送邀请时未触动。我在做什么错:
控制器/参与者/ invitations_controller.rb
class Participants::InvitationsController < Devise::InvitationsController
before_action :update_sanitized_params, only: :update
def create
binding.pry
@from = params[:from]
@subject = params[:invite_subject]
@content = params[:invite_content]
@participant = Participant.invite!(params[:user], current_member) do |u| #XXX Check if :user should be changed
u.skip_invitation = true
end
ParticipantInvitationNotificationMailer.invite_message(@participant, @from, @subject, @content).deliver if @participant.errors.empty?
@participant.invitation_sent_at = Time.now.utC# mark invitation as delivered
if @participant.errors.empty?
flash[:notice] = "successfully sent invite to #{@participant.email}"
respond_with @participant, :location => root_path
else
render :new
end
end
def update
respond_to do |format|
format.js do
invitation_token = Devise.token_generator.digest(resource_class, :invitation_token, update_resource_params[:invitation_token])
self.resource = resource_class.where(invitation_token: invitation_token).first
resource.skip_password = true
resource.update_attributes update_resource_params.except(:invitation_token)
end
format.html do
super
end
end
end
protected
def update_sanitized_params
devise_parameter_sanitizer.permit(:accept_invitation, keys: [:password, :password_confirmation, :invitation_token, :username])
end
end
的config/routes.rb中
Rails.application.routes.draw do
devise_for :members, controllers: { invitations: "members/invitations" }
devise_for :participants, controllers: { invitations: "participants/invitations" }
end
型号/ participant.rb
class Participant < ApplicationRecord
attr_reader :raw_invitation_token
end
邮寄/ notification_mailer.rb
class NotificationMailer < ApplicationMailer
def invite_message(user, from, subject, content)
@user = user
@token = user.raw_invitation_token
invitation_link = accept_user_invitation_url(:invitation_token => @token)
mail(:from => from, :bcc => from, :to => @user.email, :subject => subject) do |format|
content = content.gsub '{{first_name}}', user.first_name
content = content.gsub '{{last_name}}', user.last_name
content = content.gsub '{{full_name}}', user.full_name
content = content.gsub('{{invitation_link}}', invitation_link)
format.text do
render :text => content
end
end
end
end
如果我发送一个邀请:与Participant.invite!({:email => '[email protected]'}, Member.first)
邀请是通过默认的邮件发送通过我的新邮件显示在控制台,但没有。为什么?
Rendering /Users/andres/.rvm/gems/[email protected]/gems/devise_invitable-1.7.1/app/views/devise/mailer/invitation_instructions.html.erb
Rendered /Users/andres/.rvm/gems/[email protected]/gems/devise_invitable-1.7.1/app/views/devise/mailer/invitation_instructions.html.erb (0.6ms)
Rendering /Users/andres/.rvm/gems/[email protected]/gems/devise_invitable-1.7.1/app/views/devise/mailer/invitation_instructions.text.erb
Rendered /Users/andres/.rvm/gems/[email protected]/gems/devise_invitable-1.7.1/app/views/devise/mailer/invitation_instructions.text.erb (0.8ms)
答
最后,我可以解决这个问题。
它最终成为一个菜鸟的错误,我认为调用invite!
方法将与自定义邀请控制器中的自定义create
方法有任何关系。
我当然要通过指定的路线达到create
方法,并在该方法内阻止邀请!方法通过使用下面的代码默认邮件发送电子邮件(如设计Invitable文件明确规定):
@participant = Participant.invite!({:email => @invitation_draft.email}, current_member) do |u|
u.skip_invitation = true
end
此之后,我们可以调用的方法创建的任何自定义邮件。