私のアプリケーションには、User_mailer、prduct_mailer、some_other_mailer の 3 つのメーラーがあり、それらはすべて app/views/user_mailer にビューを保存しています ...
/app/views/ に mailers という名前のサブディレクトリを作成し、すべてを user_mailer、product_mailer、some_other_mailer フォルダーに配置します。
ありがとう、
私のアプリケーションには、User_mailer、prduct_mailer、some_other_mailer の 3 つのメーラーがあり、それらはすべて app/views/user_mailer にビューを保存しています ...
/app/views/ に mailers という名前のサブディレクトリを作成し、すべてを user_mailer、product_mailer、some_other_mailer フォルダーに配置します。
ありがとう、
実際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
)。
この組織戦略に大賛成!
そして、のび太の例から、私は次のようにしてそれを達成しました:
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 固有のものですが、それほど悪くはありません!
3.1でこれで運が良かった
class UserMailer < ActionMailer::Base
...
append_view_path("#{Rails.root}/app/views/mailers")
...
end
template_root と RAILS_ROOT で非推奨の警告を受け取りました
本当に柔軟なものが必要な場合は、継承が役立ちます。
class ApplicationMailer < ActionMailer::Base
def self.inherited(subclass)
subclass.default template_path: "mailers/#{subclass.name.to_s.underscore}"
end
end
テンプレートはどこにでも配置できますが、メーラーで指定する必要があります。このようなもの:
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メーラービューをご覧ください。