27

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?

AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

4 Answers4

44

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)

Andrew
  • 12,991
  • 15
  • 55
  • 85
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
  • Andrew, thanks but I want this for one mailer, not all. Where would this go when I have something like def XXX mail(:to => ....) end – AnApprentice Mar 07 '11 at 01:21
  • See updated answer. There is, always, more detail in the API as well. – Andrew Marshall Mar 07 '11 at 01:24
  • 5
    unfortunately `layout 'my_layout', except: 'action'` does not work for me. I have to use `mail([…]) do |format| format.text {render layout: nil}} end` in the action. – Sandro L Aug 19 '14 at 07:08
5

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
pastullo
  • 4,171
  • 3
  • 30
  • 36
4

The layout method can accept the name of a method; use the method to determine whether to show a layout and return that name or false.

layout :choose_layout
...

private
def choose_layout
  if something
    return false
  else
    return 'application'
  end
end
DGM
  • 26,629
  • 7
  • 58
  • 79
3

You could also be very sketchy and do this before the mail( ) call at the end of the specific mailer action instead:

@_action_has_layout = false
parreirat
  • 715
  • 3
  • 9
  • 22