我想创建一个按钮,在按下时向用户发送电子邮件 - 所以我使用ActionMailer - http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails 如第2节至第2.1.4段所述,我创建了:
应用/邮寄者/ user_mailer.rb
class UserMailer < ActionMailer::Base
default from: 'myemail@gmail.com'
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
所以在这里,我不知道在@url中放入什么价值?
然后我为我的电子邮件创建了一个视图 - 但我不知道在我的用户模型中要调用什么来告诉它在用户点击按钮时发送电子邮件,这是我的 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
现在在我的观点中,我希望用户在按下此按钮时收到一封电子邮件 -
<button class="btn btn-default" type="button">Get my reward</button>
答案 0 :(得分:1)
从User
模型中,您可以调用UserMailer.send_email(self)
。您可以将其放入方法中并使用after_save
回调进行调用,以便在User
更新其个人资料时发送电子邮件。