I have a table CLIENTS with id, name and email fields and I am sending them emails using ActionMailer with 3rd party SMTP.
Now I want the clients to have subscription option too so I added "subscription" column with default value as true.
Now how to generate a link which can be put in views mailer template so when the user clicks on it, the subscription value changes to false so in future the client dont' get any email ? Do note that these clients are not my rails app users so I can't uses what is been suggested here Rails 3.2 ActionMailer handle unsubscribe link in emails
I found this link how to generate link for unsubscribing from email too which looked helpful but I thought may be in 3 years, we might have got a better solution
Here is my Complete Code -
#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
Here is Clients_controller.rb file
# 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
The code is working fine for existing records and the unsubscription is working perfectly, I am only having problem in creating new clients.
I have used @client in unsubscribe method as I am using this object in client_mailer.rb template (using @client or just using client, both are working!)
EDIT 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 %>
I have copied the full track stack at http://jsfiddle.net/icyborg7/dadGS/