赫謙小便籤

用Subdomain時自定Devise的Mailer Template

這問題其實也是忽然間被雷到 … 在這樣做之前要先去修改 config/initializers/devise.rb,設定自定的Mailer,然後我們還要自己手動產生一個Mailer。

我原本的作法是用Namespace來寫整個站,所以View的分類就是已經有分好資料夾了,我直接貼code可以嗎XD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  %w'confirmation_instructions reset_password_instructions unlock_instructions'.each do |method|
    define_method "#{method}" do |record|
      send_mail_with_record_and_action(record, method.to_s)
    end
  end

  private

  def send_mail_with_record_and_action(record, action)
    initialize_from_record(record)
    headers = headers_for(action.to_s)
    headers[:template_path] = "#{record.from_app}/mailer"
    mail headers
  end

由於devise有confirmation_instructionsreset_password_instructionsunlock_instructions三個,而我只是要導到不同的app目錄去,所以就這樣子refactor,讓headers中的template_path可以是自訂的目錄。

Comments