在邮箱邮件通知中访问current_user

时间:2017-01-10 09:37:58

标签: ruby-on-rails ruby devise actionview mailboxer

我正在尝试将用户名打印到通过rails g mailboxer:views创建的邮件模板中。

每当我尝试调用该方法时,都会引发“找不到方法或变量current_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

它以同样的方式解决。

1 个答案:

答案 0 :(得分:0)

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

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

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