Rails 3.2 ActionMailer处理电子邮件中的取消订阅链接

时间:2012-03-06 21:22:45

标签: ruby-on-rails-3 ruby-on-rails-3.1 actionmailer

我有一个客户列表,我使用ActionMailer邮件发送电子邮件。我正在尝试编写每封电子邮件底部的链接,以便客户可以取消订阅邮件。到目前为止,我的客户数据库中有一个opted_out字段,我只是不确定如何正确设置路由并让它更新正确的数据库字段。我想设计这个,这样用户只需点击链接和中提琴,取消订阅。

in blast_mailer.rb

  def mail_blast(customer, blast)
    @customer = customer
    @blast =blast
#what conditional statement goes here?
    mail(:to => @customer.email, :subject => @blast.subject)
    @blast.update_attributes(:last_sent => DateTime.now)
  end
mail_blast.html.erb中的

 <div style="font-family:Helvetica; font-size:12pt; font-style:italic; width:500px; height:auto;">
        <img src="http://www.ratatouillecatering.com/<%=asset_path("emailheader.png")%>" alt="Menu" />
        <br />
        <br />
        Dear <%= @customer.fname %> <br />

        <%= raw(@blast.content) =%> <br />
#what goes here? a link_to what?
        <br />
    </div>

1 个答案:

答案 0 :(得分:3)

我会在users表中有一个名为subscribed的布尔字段。这样,您就可以选择订阅该电子邮件的所有用户。

User.where(:subscribed => true)

然后,您可以在控制器中设置unsubscribe动作来翻转布尔值。

def unsubscribe
  User.find(params[:id]).update_attributes(:subscribed => false)
end

您所要做的就是在电子邮件模板中传递此操作的链接,并将用户的ID传入其中。可以设置路线,使URL看起来像www.example.com/users/<id>/unsubscribe