问题
I'm trying to print user name into a mail template which has been created via rails g mailboxer:views.
Whenever I try to call the method, raises an "method or variable current_user not found", although public methods as User.id do prints.
Here's the view
<!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>
And the model
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
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
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
UPDATE: It is the same issue than this one. Show username on Mailboxer inbox in Rails
And it is solved the same way.
回答1:
You do not have access to controller helpers from the mailer view.
Instead you need to use attributes of @message
object.
<h1>You have a new reply from <%= @message.sender.name %>: <%= @subject %></h1>
来源:https://stackoverflow.com/questions/41565550/accessing-current-user-in-mailboxer-mailer-notification