如何为Actionmailer的电子邮件生成取消订阅链接?

iCy*_*org 1 ruby-on-rails ruby-on-rails-3

我有一个包含id,name和email字段的表CLIENTS,我使用ActionMailer和第三方SMTP向他们发送电子邮件.

现在我希望客户端也有订阅选项,所以我添加了"subscription"列,默认值为true.

现在如何生成一个可以放在视图邮件模板中的链接,这样当用户点击它时,订阅值会变为false,所以将来客户端不会收到任何电子邮件?请注意,这些客户端不是我的rails应用程序用户,因此我无法使用此处建议的Rails 3.2 ActionMailer处理电子邮件中的取消订阅链接

我发现这个链接如何生成链接以取消订阅电子邮件,这看起来很有帮助,但我想可能在3年内,我们可能有更好的解决方案

这是我的完整代码 -

#client.rb

attr_accessible :name, :company, :email

belongs_to :user
has_many :email_ids
has_many :emails, :through => :email_ids

before_create :add_unsubscribe_hash

private

def add_unsubscribe_hash
  self.unsubscribe_hash = SecureRandom.hex
end  
Run Code Online (Sandbox Code Playgroud)

这是Clients_controller.rb文件

# clients_controller.rb

  def new
    @client = Client.new

    respond_to do |format|
      format.html
      format.json { render json: @client }
      format.js
    end
  end

  def create
    @client = current_user.clients.new(params[:client])
    respond_to do |format|
      if @client.save
        @clients = current_user.clientss.all
        format.html { redirect_to @client }
        format.json { render json: @client }
        format.js
      else
        @clients = current_user.clients.all
        format.html { render action: "new" }
        format.json { render json: @client.errors, status: :error }
        format.js
      end
    end
  end

def unsubscribe
  @client = Client.find_by_unsubscribe_hash(params[:unsubscribe_hash])
  @client.update_attribute(:subscription, false)
end
Run Code Online (Sandbox Code Playgroud)

代码对现有记录工作正常,取消订阅工作正常,我只是在创建新客户端时遇到问题.

我在unsubscribe方法中使用了@client,因为我在client_mailer.rb模板中使用此对象(使用@client或只使用客户端,两者都正常工作!)

编辑2 - _form.html.erb

<%= simple_form_for(@client, :html => {class: 'form-horizontal'}) do |f| %>

    <%= f.input :name, :label => "Full Name" %>
    <%= f.input :company %>
    <%= f.input :email %>
    <%= f.button :submit, class: 'btn btn-success' %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

我在http://jsfiddle.net/icyborg7/dadGS/上复制了完整的轨道堆栈

zea*_*soi 6

尝试将每个客户端与唯一但不起眼的标识符相关联,该标识符可用于通过电子邮件中包含的取消订阅链接查找(和取消订阅)用户.

首先在client表中添加另一列,称为unsubscribe_hash:

# from command line
rails g migration AddUnsubscribeHashToClients unsubscribe_hash:string
Run Code Online (Sandbox Code Playgroud)

然后,将随机散列与每个客户端关联:

# app/models/client.rb
before_create :add_unsubscribe_hash

private

def add_unsubscribe_hash
    self.unsubscribe_hash = SecureRandom.hex
end
Run Code Online (Sandbox Code Playgroud)

创建一个控制器操作,将subscription布尔值切换为true:

# app/controllers/clients_controller.rb
def unsubscribe
    client = Client.find_by_unsubscribe_hash(params[:unsubscribe_hash])
    client.update_attribute(:subscription, false)
end
Run Code Online (Sandbox Code Playgroud)

将它连接到一条路线:

# config/routes.rb
match 'clients/unsubscribe/:unsubscribe_hash' => 'clients#unsubscribe', :as => 'unsubscribe'
Run Code Online (Sandbox Code Playgroud)

然后,当客户端对象传递给ActionMailer时,您将可以访问该unsubscribe_hash属性,您可以通过以下方式将其传递给链接:

# ActionMailer view
<%= link_to 'Unsubscribe Me!', unsubscribe_url(@user.unsubscribe_hash) %>
Run Code Online (Sandbox Code Playgroud)

单击链接后,unsubscribe将触发操作.将通过传入来查找客户端,unsubscribe_hash并且subscription将转向该属性false.

更新:

unsubscribe_hash为现有客户端添加属性值:

# from Rails console
Client.all.each { |client| client.update_attribute(:unsubscribe_hash, SecureRandom.hex) }
Run Code Online (Sandbox Code Playgroud)