在邮箱邮件通知中访问 current_user

accessing current_user in mailboxer mailer notification

我正在尝试将用户名打印到通过 rails g mailboxer:views.

创建的邮件模板中

每当我尝试调用该方法时,都会引发 "method or variable current_user not found",尽管 public 方法作为 User.id 进行打印。

这是风景

    <!DOCTYPE html>
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
  </head>
  <body>
    <h1>You have a new reply from <%=  current_user.name %>: <%= @subject %></h1>
    <blockquote>
      <p>
        <%= raw @message.body %>
      </p>
    </blockquote>
    <p>
      Visit your inbox for more info.
    </p>
  </body>
</html>

和模型

class ConversationMessage
  include ActiveModel::Model
  include Sanitizable

  # Attributes
  attr_accessor :sender, :body, :conversation


  # Validations
  validates_presence_of :body

  # Sanitize contenteditable attributes
  sanitize_html :body

  def save
    if valid?
      ApplicationRecord.transaction do
        # In a transaction, as mailboxer performs many inserts
        sender.reply_to_conversation(conversation, body)
      end
    end
  end

  def mailbox_full_name
    full_name
  end
end

和mailboxer.rb

Mailboxer.setup do |config|

  #Configures if you application uses or not email sending for Notifications and Messages
  config.uses_emails = true

  #Configures the default from for emails sent for Messages and Notifications
  config.default_from = Settings.env.mailer.sender

  #Configures the methods needed by mailboxer
  config.email_method = :mailbox_email
  config.name_method  = :mailbox_full_name

  #Configures if you use or not a search engine and which one you are using
  #Supported engines: [:solr,:sphinx]
  config.search_enabled = false
  config.search_engine = :solr

  #Configures maximum length of the message subject and body
  config.subject_max_length = 255
  config.body_max_length = 32000
end

还有一个问题用于自定义方法 mailbox_email 和 mailbox_name

module DeliveryMessage::Mailboxable
  extend ActiveSupport::Concern

  included do
    acts_as_messageable

    def mailbox_full_name
      full_name
    end

    #Returning the email address of the model if an email should be sent for this object (Message or Notification).
    #If no mail has to be sent, return nil.
    def mailbox_email(object)
      email
    end
  end
end

更新:这是同一个问题。 Show username on Mailboxer inbox in Rails

同样的方法解决了

您无权从邮件程序视图访问控制器助手。

相反,您需要使用 @message 对象的属性。

<h1>You have a new reply from <%= @message.sender.name %>: <%= @subject %></h1>