简体   繁体   中英

How can I let users chose their own email template?

So I have an application where users can create emails, and I'd like them to be able to chose their email template design.

How might I implement this? I understand it may be a bit complex perhaps but I'm open to ideas.

newsletter.rb

class Newsletter < ActiveRecord::Base
  has_many :subscriptions
  has_many :users, through: :subscriptions
  has_many :posts, dependent: :destroy
  belongs_to :user
  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
  validates_presence_of :image, :name

  scope :for, ->(user) do
    if user 
      Newsletter.all
    end
  end

end

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, 
         :validatable

  has_many :subscriptions
  has_many :subscribed_newsletters, through: :subscriptions, class_name: "Newsletter"
  has_many :newsletters

  validates :email, uniqueness: true, presence: true
  validates_presence_of :password, on: :create         

end

post.rb <---- this is what would go inside the template, the content from the post model.

class Post < ActiveRecord::Base
  belongs_to :newsletter
end

Lets assume it's a user preference of which template they wish to be sent.

User.newsletter_template = 'my_custom_template'

Now, create a generic activemailer template that simply renders the user's chosen template

newsletter_email.html.erb

<%= render "/some/path/#{user.newsletter_template}.html.erb" %>

my_custom_template.html.erb

<html>stuff</html>

my_other_template.html.erb

<html>alt template</html>

I know this isn't an in-depth explanation. Don't be shy to ask!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM