27

私のアプリケーションには、User_mailer、prduct_mailer、some_other_mailer の 3 つのメーラーがあり、それらはすべて app/views/user_mailer にビューを保存しています ...

/app/views/ に mailers という名前のサブディレクトリを作成し、すべてを user_mailer、product_mailer、some_other_mailer フォルダーに配置します。

ありがとう、

4

6 に答える 6

36

実際ApplicationMailerには、デフォルトでクラスを作成し、メーラーでそれを継承する必要があります。

# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
  append_view_path Rails.root.join('app', 'views', 'mailers')
  default from: "Whatever HQ <hq@whatever.com>"
end

# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
  def say_hi(user)
    # ...
  end
end

# app/views/mailers/user_mailer/say_hi.html.erb
<b>Hi @user.name!</b>

この素敵なパターンは、コントローラーと同じ継承スキームを使用します (例: ApplicationController < ActionController::Base)。

于 2015-04-22T18:27:34.003 に答える
24

この組織戦略に大賛成!

そして、のび太の例から、私は次のようにしてそれを達成しました:

class UserMailer < ActionMailer::Base
  default :from => "whatever@whatever.com"
  default :template_path => '**your_path**'

  def whatever_email(user)
    @user = user
    @url  = "http://whatever.com"
    mail(:to => user.email,
         :subject => "Welcome to Whatever",
         )
  end
end

これは Mailer 固有のものですが、それほど悪くはありません!

于 2013-09-02T18:32:55.440 に答える
14

3.1でこれで運が良かった

class UserMailer < ActionMailer::Base
  ...
  append_view_path("#{Rails.root}/app/views/mailers")
  ...
end 

template_root と RAILS_ROOT で非推奨の警告を受け取りました

于 2012-06-05T04:39:56.897 に答える
14

本当に柔軟なものが必要な場合は、継承が役立ちます。

class ApplicationMailer < ActionMailer::Base

  def self.inherited(subclass)
    subclass.default template_path: "mailers/#{subclass.name.to_s.underscore}"
  end

end
于 2015-10-23T22:28:58.987 に答える
4

テンプレートはどこにでも配置できますが、メーラーで指定する必要があります。このようなもの:

class UserMailer < ActionMailer::Base
  default :from => "whatever@whatever.com"

  def whatever_email(user)
    @user = user
    @url  = "http://whatever.com"
    mail(:to => user.email,
         :subject => "Welcome to Whatever",
         :template_path => '**your_path**',
         )
  end
end

詳細については、2.4メーラービューをご覧ください。

于 2012-04-04T19:41:07.007 に答える