在process_action回调之前:尚未定义require_no_authentication

时间:2017-11-03 07:35:22

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

我试图在第一次注册时确认用户,但是用户一直收到此错误。一旦他收到确认电子邮件并点击它。他得到以下错误

error.log中

Started GET "/users/confirmation?confirmation_token=zu1phZ8TaJHNFDJcXWvE" for 127.0.0.1 at 2017-11-03 13:00:02 +0530
   (4.2ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC

ArgumentError (Before process_action callback :require_no_authentication has not been defined):

app/controllers/confirmations_controller.rb:2:in `<class:ConfirmationsController>'
app/controllers/confirmations_controller.rb:1:in `<top (required)>'

confirmations_controller.rb

class ConfirmationsController <  Devise::ConfirmationsController
  skip_before_action :require_no_authentication!
  skip_before_action :authenticate_user!

end

首先,我认为我没有 require_no_authentication!方法。所以我尝试将这个方法的代码放在控制器本身,但它还没有工作。

class ConfirmationsController <  Devise::ConfirmationsController
  skip_before_action :require_no_authentication!
  skip_before_action :authenticate_user!

  private
  def require_no_authentication!
    assert_is_devise_resource!
    return unless is_navigational_format?
    no_input = devise_mapping.no_input_strategies

    authenticated = if no_input.present?
      args = no_input.dup.push scope: resource_name
      warden.authenticate?(*args)
    else
      warden.authenticated?(resource_name)
    end

    if authenticated && resource = warden.user(resource_name)
      flash[:alert] = I18n.t("devise.failure.already_authenticated")
      redirect_to after_sign_in_path_for(resource)
    end
  end

end

的routes.rb

Rails.application.routes.draw do
  devise_for :users, controllers: {
    sessions: 'api/v1/sessions',
    registrations: 'api/v1/registrations',
    passwords: 'api/v1/passwords',
    :confirmations => "confirmations" 
  }
end

2 个答案:

答案 0 :(得分:0)

似乎应该只是:

:require_no_authentication

:require_no_authentication!

......至少从我刚才读到的Devise docs

开始

答案 1 :(得分:0)

这是解决方案。我没有跳过我检查用户是否有authentication_token的require_login方法

class ConfirmationsController < Devise::ConfirmationsController
  skip_before_action :require_login!

  def new
    super
  end

  def create
    super
  end

  def show
    self.resource = resource_class.confirm_by_token(params[:confirmation_token])

    if resource.errors.empty?
      set_flash_message(:notice, :confirmed) if is_navigational_format?
      sign_in(resource_name, resource)
      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

  private

  def after_confirmation_path_for(resource_name, resource)
    if signed_in?(resource_name)
      signed_in_root_path(resource)
    else
      new_session_path(resource_name)
    end
  end
end