Reputation: 453
I would like to create a button that send an email to a User when pressed - So i Used ActionMailer - http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails As described from section 2 to 2.1.4 I created:
app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default from: '[email protected]'
end
def send_email(user)
@user = user
@url = 'http://example.com/login'
mail(to: @user.email, subject: 'You receive this email because you click on the btn)
end
end
So here I don't know what value to put inside @url ?
Then i created a view for my email - But I don't know what to call in my user model to tell it to send the email when the user click on button, here is my app/controllers/user_controller.rb
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
@users = User.all
@users = User.paginate(:page => params[:page], :per_page => 50)
end
# GET /users/1
# GET /users/1.json
def show
@user = User.find(params[:id])
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render action: 'show', status: :created, location: @user }
else
format.html { render action: 'new' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params[:user]
end
def self.find_for_facebook_oauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
end
end
end
Now In my views, I would like the user to receive an email when he is cliking on this button -
<button class="btn btn-default" type="button">Get my reward</button>
Upvotes: 0
Views: 1907
Reputation: 4536
From the User
model you would call UserMailer.send_email(self)
. You could put that in a method and call that using a after_save
callback so that the email is sent whenever the User
updates his/her profile.
Upvotes: 1