【问题标题】:How can I let users chose their own email template?如何让用户选择自己的电子邮件模板?
【发布时间】:2015-02-06 06:32:15
【问题描述】:

所以我有一个用户可以创建电子邮件的应用程序,我希望他们能够选择他们的电子邮件模板设计。

我该如何实现呢?我知道这可能有点复杂,但我对想法持开放态度。

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

用户.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

class Post < ActiveRecord::Base
  belongs_to :newsletter
end

【问题讨论】:

  • 你问的是如何实现模型关系或视图?
  • 干杯@eabraham!两者兼而有之,我不确定是否应该为此生成一个新模型,即 template.rb,最终,当用户为他们的时事通讯创建帖子时,我想让他们能够选择他们的电子邮件模板。因此,任何关于该做什么或从哪里开始完成此任务的提示都会非常有帮助!

标签: jquery html ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

假设这是他们希望发送哪个模板的用户偏好。

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>

我知道这不是一个深入的解释。不要害羞!

【讨论】:

  • 谢谢,+1 这很有帮助!我确实有几个问题。 1、newsletter_template方法在哪里定义?它是用户模型方法还是我应该创建一个单独的 newsletter_template 模型? 2、用户实际是如何选择模板的? IE。复选框?或者这将如何工作?
  • 1.为 User 模型创建一个属性。随意命名。 2. 我可能会使用
  • 好的,很好,只是为了清楚这将是用户表或帖子表上的一个新列,在我的情况下,即 newsletter_template:string,我会用我的时事通讯预先填充它,然后在选择用户可以用模板选择表格吗?我会接受的。
猜你喜欢
  • 2020-04-03
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 2016-09-10
  • 2012-05-17
相关资源
最近更新 更多