在邮箱邮件程序通知中访问current_user [英] accessing current_user in mailboxer mailer notification
问题描述
我试图将用户名打印到已通过rails g mailer:views创建的邮件模板中。
I'm trying to print user name into a mail template which has been created via rails g mailboxer:views.
每当我尝试调用该方法时,都会引发尽管会以User。 id 的公共方法进行打印,但仍会显示未找到方法或变量current_user。
Whenever I try to call the method, raises an "method or variable current_user not found", although public methods as User.id do prints.
这里是视图
<!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
And the 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
There's also a concern which is used for the custom methods mailbox_email and 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
更新:这是同一问题。 在Rails的Mailboxer收件箱中显示用户名
UPDATE: It is the same issue than this one. Show username on Mailboxer inbox in Rails
它的解决方法相同。
推荐答案
您无权访问控制器
相反,您需要使用 @message
对象的属性。
Instead you need to use attributes of @message
object.
<h1>You have a new reply from <%= @message.sender.name %>: <%= @subject %></h1>
这篇关于在邮箱邮件程序通知中访问current_user的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!