所以我有一个用户可以创建电子邮件的应用程序,我希望他们能够选择他们的电子邮件模板设计。
我该如何实现?我知道它可能有点复杂,但我对想法持开放态度。
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&lt; ----这是模板内部的内容,来自帖子模型的内容。
class Post < ActiveRecord::Base
belongs_to :newsletter
end
答案 0 :(得分:2)
让我们假设用户偏好他们希望发送哪个模板。
User.newsletter_template = 'my_custom_template'
现在,创建一个通用的activemailer模板,只需呈现用户选择的模板
<强> 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>
我知道这不是一个深入的解释。不要害羞地问!