Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails mailer views in separated directory

I have small organizatoric issue, in my application I have 3 mailer User_mailer, prduct_mailer, some_other_mailer and all of them store their views in app/views/user_mailer ...

I will want to have a subdirectory in /app/views/ called mailers and put all in the folders user_mailer, product_mailer and some_other_mailer.

Thanks,

like image 803
Calin Avatar asked Apr 04 '12 19:04

Calin


People also ask

How do I view Mailers in Rails?

Mailer views are located in the app/views/name_of_mailer_class directory. The specific mailer view is known to the class because its name is the same as the mailer method. In our example from above, our mailer view for the welcome_email method will be in app/views/user_mailer/welcome_email. html.

What is Action_ mailer in Rails?

Action Mailer is the Rails component that enables applications to send and receive emails. In this chapter, we will see how to send an email using Rails. Let's start creating an emails project using the following command. tp> rails new mailtest. This will create the required framework to proceed.


2 Answers

You should really create an ApplicationMailer class with your defaults and inherit from that in your mailers:

# app/mailers/application_mailer.rb class ApplicationMailer < ActionMailer::Base   append_view_path Rails.root.join('app', 'views', 'mailers')   default from: "Whatever HQ <[email protected]>" 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> 

This lovely pattern uses the same inheritance scheme as controllers (e.g. ApplicationController < ActionController::Base).

like image 115
fny Avatar answered Oct 13 '22 23:10

fny


I so agree with this organization strategy!

And from Nobita's example, I achieved it by doing:

class UserMailer < ActionMailer::Base   default :from => "[email protected]"   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 

It is Mailer-specific but not too bad!

like image 35
Augustin Riedinger Avatar answered Oct 13 '22 23:10

Augustin Riedinger