目前我有ActionMailer在用户注册时发送电子邮件,并且我与用户生成随机:sign_in_token。
我还创建了一个名为active的方法来切换用户:当他们点击电子邮件中的链接时,registration_complete布尔值为TRUE。
如何设置网址,以便用户点击该链接并使用我的有效方法更改用户价值?
目前,我可以发送链接并生成随机令牌,但我不知道如何让链接调用我的主动方法。
模型
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :sign_in_token,
:registration_complete
###This generates my sign_in_token
def generate_sign_in_token
self.sign_in_token = Digest::SHA1.hexdigest([Time.now, rand].join)
end
end
CONTROLLER
def create
@user = RegularUser.new(params[:regular_user])
if @user.save
###Sends the User an email with sign_in_token
UserMailer.registration_confirmation(@user, login_url+"/#{@user.sign_in_token}").deliver
flash[:success] = "Please Check Your Email to Verify your Registration!"
redirect_to (verifyemail_path)
else
render 'new'
end
end
###method that finds the user and sets the boolean value to true.
def activate
@user = User.find_by_sign_in_token!(params[:sign_in_token])
if @user.update_attribute(registration_complete: true)
redirect_to login_url
flash[:success] = "Email has been Verified."
end
end
user_mailer文件
def registration_confirmation(user, login_url)
@login_url = login_url
@user = user
mail(:to => "#{user.name} <#{user.email}>", :subject => "Welcome to APP")
end
视图
###Redirects User to Login Page, But how do i connect it to my activate method or user?
<%= link_to "Complete Registration", @login_url %>
路线
match '/login/:sign_in_token', :to => 'sessions#new'
答案 0 :(得分:2)
最简单的解决方案是让路线直接通过邮件将它们发送到激活操作。我可能会建立一条完全不同的路线,如:
match '/activate/:sign_in_token', :to => 'sessions#activate', via: [:post]
然后使用/ activate而不是/ login发送用户链接。
编辑:顺便说一句,在您的激活方法中,您必须添加一些额外的逻辑来处理已激活和无效的URL案例。