AnApprentice
AnApprentice

Reputation: 111080

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

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?

Upvotes: 30

Views: 10795

Answers (4)

pastullo
pastullo

Reputation: 4201

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

Upvotes: 5

Andrew Marshall
Andrew Marshall

Reputation: 97004

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)

Upvotes: 48

parreirat
parreirat

Reputation: 715

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

Upvotes: 3

DGM
DGM

Reputation: 26979

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

Upvotes: 4

Related Questions