Ruby on rails 设计可确认:令牌无效错误
已经找了3个小时了。。。建立可证实的电子邮件,它被认为是容易的,但它远远不是。不管怎么说,我已经克服了大量的错误、未定义的方法和变量,并努力通过这件事来给我一些东西。。。但是我不能证实。我在确认控制器的引擎盖下查看了一下,但没有结果。。。这根本不符合事实。我不知道我在这里做错了什么,但它始终将我重定向到“重新发送确认说明”页面 更新:获取了在其初始位置生成的错误,即臭名昭著的无效令牌错误。又一次,一个小时的研究没有解决任何问题,我一直听说你需要使用@token,就像我认为的那样。。。它本应该让它发挥作用,但事实并非如此 控制器:默认设计确认控制器:Ruby on rails 设计可确认:令牌无效错误,ruby-on-rails,devise,devise-confirmable,Ruby On Rails,Devise,Devise Confirmable,已经找了3个小时了。。。建立可证实的电子邮件,它被认为是容易的,但它远远不是。不管怎么说,我已经克服了大量的错误、未定义的方法和变量,并努力通过这件事来给我一些东西。。。但是我不能证实。我在确认控制器的引擎盖下查看了一下,但没有结果。。。这根本不符合事实。我不知道我在这里做错了什么,但它始终将我重定向到“重新发送确认说明”页面 更新:获取了在其初始位置生成的错误,即臭名昭著的无效令牌错误。又一次,一个小时的研究没有解决任何问题,我一直听说你需要使用@token,就像我认为的那样。。。它本应该让它
class ConfirmationsController < Devise::ConfirmationsController
# GET /resource/confirmation/new
def new
self.resource = resource_class.new
end
# POST /resource/confirmation
def create
self.resource = resource_class.send_confirmation_instructions(resource_params)
yield resource if block_given?
if successfully_sent?(resource)
respond_with({}, location: after_resending_confirmation_instructions_path_for(resource_name))
else
respond_with(resource)
end
end
# GET /resource/confirmation?confirmation_token=abcdef
def show
self.resource = resource_class.confirm_by_token(params[:confirmation_token])
yield resource if block_given?
if resource.errors.empty?
set_flash_message(:notice, :confirmed) if is_flashing_format?
respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) }
else
respond_with_navigational(resource.errors, status: :unprocessable_entity){ render :new }
end
end
protected
# The path used after resending confirmation instructions.
def after_resending_confirmation_instructions_path_for(resource_name)
new_session_path(resource_name) if is_navigational_format?
end
# The path used after confirmation.
def after_confirmation_path_for(resource_name, resource)
if signed_in?(resource_name)
authenticated_root_path(resource)
else
unauthenticated_root_path
end
end
end
# GET /resource/confirmation?confirmation_token=abcdef
def show
self.resource = resource_class.confirm_by_token(params[:confirmation_token])
yield resource if block_given?
if resource.errors.empty?
set_flash_message(:notice, :confirmed) if is_flashing_format?
respond_to do |format|
format.html{redirect_to unauthenticated_root_path}
format.mobile {redirect_to unauthenticated_root_path, notice: 'Confirmation success!'}
end
else
respond_to do |format|
format.html{render :new}
format.mobile {redirect_to unauthenticated_root_path, alert: 'Failure, already confirmed'}
end
end
end
新确认书
<h2>Resend confirmation instructions</h2>
<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %></div>
<div><%= f.submit "Resend confirmation instructions" %></div>
<% end %>
<%= devise_error_messages!(:email) %>
<%= render "devise/shared/links" %>
重新发送确认指令
此外,我还修改了Desive error helper,使其显示自定义错误(不知道可能是什么把它搞砸了,但它在这里:
module DeviseHelper
def devise_error_messages!(field)
return nil if resource.errors.empty?
messages = resource.errors.full_messages_for(field).map { |msg| content_tag(:li, msg) }.join
if resource.errors.full_messages_for(field) != []
html = <<-HTML
<div class="alert alert-error alert-block"> <button type="button"
class="close" data-dismiss="alert">x</button>
#{messages}
</div>
HTML
html.html_safe
else
return nil
end
end
end
模块设备帮助器
def设计错误消息!(字段)
如果resource.errors.empty返回nil?
messages=resource.errors.full_messages_for(field).map{{| msg | content_tag(:li,msg)}.join
if resource.errors.full_messages_for(字段)!=[]
html=好的,经过10个小时的战斗后,我决定从头开始重新启动,并在本地和heroku上都能运行。遵循Rails 4的这个tut,设计3.2.4:。然后我有一段时间没有定义路由器错误,所以我查看了registrations控制器,并替换了默认版本中的6或7行墨迹到另一个图坦卡蒙,更好地解释了这一点:
注册控制器操作(工作)
维奥拉!它顺利通过了。所以只要花5分钟的事情只花了我10个小时。这就是效率
我还将其用于移动格式,以防你们中的一些人遇到问题:
确认控制员:
class ConfirmationsController < Devise::ConfirmationsController
# GET /resource/confirmation/new
def new
self.resource = resource_class.new
end
# POST /resource/confirmation
def create
self.resource = resource_class.send_confirmation_instructions(resource_params)
yield resource if block_given?
if successfully_sent?(resource)
respond_with({}, location: after_resending_confirmation_instructions_path_for(resource_name))
else
respond_with(resource)
end
end
# GET /resource/confirmation?confirmation_token=abcdef
def show
self.resource = resource_class.confirm_by_token(params[:confirmation_token])
yield resource if block_given?
if resource.errors.empty?
set_flash_message(:notice, :confirmed) if is_flashing_format?
respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) }
else
respond_with_navigational(resource.errors, status: :unprocessable_entity){ render :new }
end
end
protected
# The path used after resending confirmation instructions.
def after_resending_confirmation_instructions_path_for(resource_name)
new_session_path(resource_name) if is_navigational_format?
end
# The path used after confirmation.
def after_confirmation_path_for(resource_name, resource)
if signed_in?(resource_name)
authenticated_root_path(resource)
else
unauthenticated_root_path
end
end
end
# GET /resource/confirmation?confirmation_token=abcdef
def show
self.resource = resource_class.confirm_by_token(params[:confirmation_token])
yield resource if block_given?
if resource.errors.empty?
set_flash_message(:notice, :confirmed) if is_flashing_format?
respond_to do |format|
format.html{redirect_to unauthenticated_root_path}
format.mobile {redirect_to unauthenticated_root_path, notice: 'Confirmation success!'}
end
else
respond_to do |format|
format.html{render :new}
format.mobile {redirect_to unauthenticated_root_path, alert: 'Failure, already confirmed'}
end
end
end
如果你有更技术上正确的答案张贴,我会接受它,如果它比我的好
# GET /resource/confirmation?confirmation_token=abcdef
def show
self.resource = resource_class.confirm_by_token(params[:confirmation_token])
yield resource if block_given?
if resource.errors.empty?
set_flash_message(:notice, :confirmed) if is_flashing_format?
respond_to do |format|
format.html{redirect_to unauthenticated_root_path}
format.mobile {redirect_to unauthenticated_root_path, notice: 'Confirmation success!'}
end
else
respond_to do |format|
format.html{render :new}
format.mobile {redirect_to unauthenticated_root_path, alert: 'Failure, already confirmed'}
end
end
end