设计确认:令牌无效错误

时间:2014-08-04 21:49:11

标签: ruby-on-rails devise devise-confirmable

一直在寻找过去的3个小时...设置可确认的电子邮件它被认为很容易,但它是FAR。无论如何,我已经通过一系列错误和未定义的maethods和变量进行了斗争,并且已经摔跤了这件事给我发了些东西......但是我无法证实。我已经在确认控制器中查看了引擎盖无济于事......它只是没有确认正确。我不知道我在这里做错了什么,但它始终将我重定向到"重新发送确认说明"页...

更新:掌握臭名昭着的无效令牌错误时产生的错误。再一小时的研究没有解决任何问题,我一直听说你需要像我在我看来那样使用@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

梅勒观点:

 <p>Welcome <%= @email %>!</p>

<p>You can confirm your account email through the link below:</p>

<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>

路线:

devise_for :users, controllers: { registrations: 'registrations', sessions: 'sessions', confirmations: 'confirmations'}

devise_scope :user do
  authenticated :user do

    root :to => 'homepage#index', as: :authenticated_root
    end

  unauthenticated :user do
    root :to => 'devise/sessions#new', as: :unauthenticated_root
  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" %>

另外,我已经重新设计了设计错误帮助程序,让它显示自定义错误(不知道可能是什么搞乱了它,但现在是:

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

1 个答案:

答案 0 :(得分:1)

好吧,经过10个小时的战斗,我决定从头开始休息,并让它在本地以及heroku上运行。按照这个Tails进行Rails 4和Devise 3.2.4:https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users。然后我得到了未定义的路由器错误一段时间所以我看了注册控制器并替换了它的默认版本中找到的6或7行继承人链接到另一个tut更好地解释了它https://github.com/plataformatec/devise/wiki/How-To%3a-Redirect-to-a-specific-page-on-successful-sign-up-%28registration%29

注册控制器操作(工作)

# The path used after sign up for inactive accounts. You need to overwrite
# this method in your own RegistrationsController.
 def after_inactive_sign_up_path_for(resource)
   after_sign_in_path_for(resource) 
 end

中提琴!它经历了没有任何麻烦。所以只需要5分钟的时间只花了我10个小时。那就是效率!

此外,我还可以使用移动格式,以防万一你有些问题:

确认控制人:

# 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

如果你有更多技术上正确的答案,我会接受它,如果它比我的好!