简体   繁体   中英

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?

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 )

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

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

您也可以非常粗略,并在特定邮件程序操作结束时调用mail()之前执行此操作:

@_action_has_layout = false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM