未初始化的Constant ConfirmationsController

时间:2016-01-30 12:08:41

标签: ruby-on-rails ruby-on-rails-4 devise routing

我尝试自定义覆盖设计确认,以便新用户在收到确认电子邮件后创建密码。这是在设计维基中,可以找到here.

当我导航到确认链接时,我遇到了以下错误

  

未初始化的常量ConfirmationsController

我之前看到过这种情况,当时我把控制器类的名字(留下一个s或类似的东西)扯掉了,但是我在这里找不到类似的东西。我能想到的两个相关文件是我的控制器和我的路线,与设计相关。

这是我的控制器:

class Users::ConfirmationsController < Devise::ConfirmationsController
  # Remove the first skip_before_filter (:require_no_authentication) if you
  # don't want to enable logged users to access the confirmation page.
  skip_before_filter :require_no_authentication
  skip_before_filter :authenticate_user!

  # GET /resource/confirmation/new
  def new
    super
  end

  # POST /resource/confirmation
  # def create
  #   super
  # end

  # GET /resource/confirmation?confirmation_token=abcdef

  # PUT /resource/confirmation
  def update
    with_unconfirmed_confirmable do
      if @confirmable.has_no_password?
        @confirmable.attempt_set_password(params[:user])
        if @confirmable.valid? and @confirmable.password_match?
          do_confirm
        else
          do_show
          @confirmable.errors.clear #so that we wont render :new
        end
      else
        @confirmable.errors.add(:email, :password_already_set)
      end
    end

    if !@confirmable.errors.empty?
      self.resource = @confirmable
      render 'devise/confirmations/new' #Change this if you don't have the views on default path
    end
  end

  # GET /resource/confirmation?confirmation_token=abcdef
  def show
    with_unconfirmed_confirmable do
      if @confirmable.has_no_password?
        do_show
      else
        do_confirm
      end
    end
    unless @confirmable.errors.empty?
      self.resource = @confirmable
      render 'devise/confirmations/new' #Change this if you don't have the views on default path
    end
  end

  protected

  # The path used after resending confirmation instructions.
  def after_resending_confirmation_instructions_path_for(resource_name)
    super(resource_name)
  end

  # The path used after confirmation.
  def after_confirmation_path_for(resource_name, resource)
    super(resource_name, resource)
  end


  def with_unconfirmed_confirmable
    @confirmable = User.find_or_initialize_with_error_by(:confirmation_token, params[:confirmation_token])
    if !@confirmable.new_record?
      @confirmable.only_if_unconfirmed {yield}
    end
  end

  def do_show
    @confirmation_token = params[:confirmation_token]
    @requires_password = true
    self.resource = @confirmable
    render 'devise/confirmations/show' #Change this if you don't have the views on default path
  end

  def do_confirm
    @confirmable.confirm!
    set_flash_message :notice, :confirmed
    sign_in_and_redirect(resource_name, @confirmable)
  end
  end
end

以下是与设计相关的路线:

devise_for :users, controllers: {
    sessions: 'users/sessions',
confirmations: "confirmations"
}

as :user do
  patch '/user/confirmation' => 'confirmations#update', :via => :patch, :as => :update_user_confirmation
end

请随时询问您认为可能有用的任何其他代码。提前感谢任何想法。

1 个答案:

答案 0 :(得分:5)

你不应该去&#39;用户/确认#update&#39;?没有&#39;确认#update&#39;基于您的用户名类型:: ConfirmationsController

我通常将路由包装在命名空间中,但为了简单起见,您应该更新补丁。