undefined方法`validatable?' for nil:NilClass for Devise mapping

时间:2017-06-22 13:29:10

标签: ruby-on-rails devise

我在使用Devise编辑用户时遇到了一些麻烦。 该请求似乎是通过正确的路线发送的:

? transactions

但是我收到了这个错误:

Request parameters  
{"controller"=>"users/registrations", "action"=>"edit"}

RegistrationsController非常简单:

NoMethodError in Users::RegistrationsController#edit

undefined method `validatable?' for nil:NilClass

Extracted source (around line #170):

# Sets minimum password length to show to user
  def set_minimum_password_length
    if devise_mapping.validatable? # <- ERROR HIGHLIGHTED HERE
      @minimum_password_length = resource_class.password_length.min
    end
  end

编辑页面的视图:

class Users::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: [:create]
  before_action :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  def new
    super
  end

  # POST /resource
  def create
    super
  end

  # GET /resource/edit
  def edit
    super
  end

任何帮助都将不胜感激。

这里也是堆栈跟踪:

<h2>Edit <%= resource_name.to_s.humanize %></h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

  <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
    <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
  <% end %>

  <div class="field">
    <%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
    <%= f.password_field :password, autocomplete: "off" %>

    //deleting this conditional makes no difference.
    <% if @minimum_password_length %>
      <br />
      <em><%= @minimum_password_length %> characters minimum</em>
    <% end %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
    <%= f.password_field :current_password, autocomplete: "off" %>
  </div>

  <div class="actions">
    <%= f.submit "Update" %>
  </div>
<% end %>

<h3>Cancel my account</h3>

<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>

<%= link_to "Back", :back %>

2 个答案:

答案 0 :(得分:2)

问题在于,devise不知道您在自定义控制器中使用的资源是什么。因此,您需要使用资源名称将所有到您的自定义控制器的路由包装起来,方法是:

  # config/routes.rb

  devise_scope :user do
    root to: 'users/registrations#new'
  end

来源:https://www.rubydoc.info/github/plataformatec/devise/ActionDispatch%2FRouting%2FMapper:devise_scope

答案 1 :(得分:0)

我最近遇到了这个问题,但是当我将路线移至root 'registrations#new'而不是home#index时,这个问题似乎发展了。将它还原回去对我来说是固定的。