Weird object issue when trying to render partial from mailer view
Using Rails 3. Per first snippet of code below, it's hard to google search what this error means when trying to render a partial from my mailer view.
This is the error:
ActionView::Template::Error (undefined method `long_date' for #<#<C开发者_运维百科lass:0x00000102452ab0>:0x00000102440b30>):
Here's my mailer view (email_note.html.erb)
<div style="width:500px;">
<%= render :partial => "shared/note", :object => @note %>
</div>
This is the call i'm making in my partial that is throwing the error <%= long_date(note.created_at) %>
long_date has always worked when I have given the partial @note objects from other actions (e.g. 'show' in my notes_controller). this is the method
def long_date(date)
date.strftime('%A %b %e, %G')
end
Here is my ActionMailer class (address and note_id are sent from an 'email' action in my notes_controller)
class UserMailer < ActionMailer::Base
default :from => "gminett@gmail.com"
def email_note(address,note_id)
@note = Note.find(note_id)
mail(:to => "#{address}", :subject => "Note from repository: #{@note.subject}")
end
end
Seems to me rails (very most likely due to some oversight on my part) is not interpreting my @note object as a Note class, but i don't know how to fix it. Help is much appreciated.
This is because by default ActionMailer templates don't share the same helpers that ActionView ones do. If you need to use helpers in your mailer templates then try including the module in your ActionMailer class:
include DateTimeHelper
精彩评论