2017-02-23 20 views
0

我的應用中有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) 

回答

0

最後,我可以解決這個問題。

它最終成爲一個菜鳥的錯誤,我認爲調用invite!方法將與自定義邀請控制器中的自定義create方法有任何關係。

我當然要通過指定的路線達到create方法,並在該方法內阻止邀請!方法通過使用下面的代碼默認郵件發送電子郵件(如設計Invitable文件明確規定):

@participant = Participant.invite!({:email => @invitation_draft.email}, current_member) do |u| 
    u.skip_invitation = true 
    end 

此之後,我們可以調用的方法創建的任何自定義郵件。