views:

115

answers:

2

When using ActionMailer it uses a 'view' in the conventional path but how do you configure the 'view' that is being used (i.e. choose your own)?

It looks easy enough to change the 'layout' being used but where you do the same for the view?

A: 

So if you have a UserMailer model, then you should have a directory app/views/user_mailer. Inside there you would have views based on the method names inside your model. So if you had a welcome_email method, you could have a template welcome_email.text.html.erb (or some such).

There is a pretty good explanation (which I cribbed from) here.

Shane Liebling
Thanks but I can get the 'standard' view to work just fine I want to be able to select a custom view, not use a view named after the method. The reason being I have a mailer used for contact forms and the like and I want to render each form in a different way.
Kris
+1  A: 

Add a template command in your model. E.g.:

class UserMailer < ActionMailer::Base
  def welcome_email(user)
    recipients user.email
    from "Me<[email protected]>"
    subject "Welcome!"
    sent_on Time.now
    body {:user => user, :url => "http://example.com/login"}
    content_type "text/html"
    ## use some_other_template.text.(html|plain).erb instead
    template "some_other_template"
  end
end

Alternatively, you can also use the default views but specify a partial within the actual view, and thus use any partial you want just like any normal view.

JRL
Of note - Rails also checks all controller 'view_paths' for a template.
Kris