开发者

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.

A开发者_如何学编程ny 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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜