Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails for ActionMailer - How to disable a Layout for a certain mailer

Tags:

I have a user_mailer with a layout.

For one of my actionmailer methods I want the mailer to NOT use the default layout. But I can't see to find a setting for No Layout.

Any ideas?

like image 255
AnApprentice Avatar asked Mar 07 '11 01:03

AnApprentice


People also ask

What is Action_ mailer in Rails?

Action Mailer is the Rails component that enables applications to send and receive emails. In this chapter, we will see how to send an email using Rails. Let's start creating an emails project using the following command. tp> rails new mailtest. This will create the required framework to proceed.

How do I Preview mailers in Rails?

rails generates a mail preview if you use rails g mailer CustomMailer . You will get a file CustomMailerPreview inside spec/mailers/previews folder. Here you can write your method that will call the mailer and it'll generate a preview.

What is Default_url_options?

The default_url_options setting is useful for constructing link URLs in email templates. Usually, the :host , i.e. the fully qualified name of the web server, is needed to be set up with this config option. It has nothing to do with sending emails, it only configures displaying links in the emails.


2 Answers

Simply specify in your mailer:

layout false 

You can also append :only => my_action (or :except) to limit the methods it applies to, like so:

layout false, :only => 'email_method_no_layout' 

(applicable API documentation)

like image 130
Andrew Marshall Avatar answered Sep 17 '22 17:09

Andrew Marshall


I did it by using a small function, looking at the action name and return the correct mailer layout to be used:

class TransactionMailer < ApplicationMailer    layout :select_layout    def confirmation_email contact     #code   end    private   def select_layout     if action_name == 'confirmation_email'       false     else       'mailer'     end   end end 
like image 40
pastullo Avatar answered Sep 17 '22 17:09

pastullo