我现在已经尝试了三天而且我无法修复它。
用户“注册”后,会重定向到验证页面,用户输入短信令牌(数字位),然后在输入正确的短信令牌时确认用户。
verification_code_input.html.erb
<%= form_for :user, url: verifications_path do |f| %>
<div><%= f.label :verification_code_confirmation %><br />
<div><%= f.number_field :verification_code_confirmation %></div>
<div><%= f.submit 'Verify' %></div>
<% end %>
registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
@user = User.new(params[:user])
p = [('0'..'9')].map { |i| i.to_a }.flatten
@user.verification_code = (0...6).map{ p[rand(p.length)] }.join
respond_to do |format|
if @user.save
# render text: "Thank you! You will receive an SMS shortly with verification instructions."
# Instantiate a Twilio client
client = Twilio::REST::Client.new(TWILIO_CONFIG['sid'], TWILIO_CONFIG['token'])
# Create and send an SMS message
client.account.sms.messages.create(
from: TWILIO_CONFIG['from'],
to: "+6#{@user.phone}",
body: "Thanks for signing up. To verify your account, please enter code #{@user.verification_code} in the Clixster Store registration page."
)
format.html { redirect_to verification_code_input_path }
else
format.html { render action: "new" }
end
end
end
def update
super
end
def verification_code_input
# @code = User.find(current_user.id).verification_code
respond_to do |format|
format.html
end
end
def verification
@user = params[:id].blank? ? current_user : User.find(params[:id])
respond_to do |format|
if @user.verify_and_save(params[:user])
format.html { redirect_to root_path, notice: "Thank you for Signing up! Have a great time on our store."}
else
format.html { render action: "verification_code_input" }
end
end
end
end
的routes.rb
get "verifications/new" => "verifications#new"
post "verifications" => "verifications#create"
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :verification_code_confirmation, :phone
# attr_accessible :title, :body
def verify_and_save(attributes)
self.assign_attributes attributes
if self.verification_code == self.verification_code_confirmation
self.verified = true
self.verification_code = nil
self.save
else
self.errors.add(:verification_code_confirmation)
false
end
end
end
编辑:添加我的错误。
我有错误:
undefined method `verify_and_save' for nil:NilClass
答案 0 :(得分:0)
我认为你需要绕过,
用代码验证用户后,试试这个
sign_in @user, :bypass => true
然后使用
redirect_to root_path
答案 1 :(得分:0)
在你的创建方法中:
sign_in(:user, @user)
然后重定向到您想要的任何地方。