【问题标题】:Devise undefined method 'registration_path'设计未定义的方法'registration_path'
【发布时间】:2017-08-23 22:16:17
【问题描述】:

正在开发我的应用程序并安装和卸载几个 gem,然后突然设计的东西停止工作,我不知道为什么。我不断收到以下错误:

NoMethodError in Recipes#index
undefined method `registration_path' for #<#<Class:0x00560876ed7ef0>:0x00560876f06430>

它指的是我的注册和这段代码

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), validate: true) do |f| %>
                <%= devise_error_messages! %>
                <%= f.input :email, validate: true, required: true, placeholder: "Email", label: false %>
                <% if @minimum_password_length %>
                  <em>(<%= @minimum_password_length %> characters minimum)</em>
                <% end %>
                <%= f.input :password, autocomplete: "off", required: true, placeholder: "Password", label: false %>
                <%= f.input :password_confirmation, autocomplete: "off", required: true, placeholder: "Confirm Password", label: false %>
                <%= f.input :firstname, required: true, placeholder: "First Name", label: false %>
                <%= f.input :lastname, required: true, placeholder: "Last Name", label: false %>
                <%= f.input :displayname, validate: true, placeholder: "Display Name", label: false %>
                <div class="recaptcha">
                  <%= recaptcha_tags %>
                </div>
                <%= f.button :submit, "Sign up", class: 'btn btn-primary' %>
              <% end %>

在我的 routes.rb 我有这个:

Rails.application.routes.draw do
  devise_for :users, controllers: { registrations: 'registrations', omniauth_callbacks: "callbacks" }

这是注册控制器:

class RegistrationsController < Devise::RegistrationsController

    def sign_up_params
        params.require(:user).permit(:firstname, :lastname, :displayname, 
                                     :email, :password, :password_confirmation)
    end

    def account_update_params
        params.require(:user).permit(:firstname, :lastname, :displayname,
                                     :email, :password, :password_confirmation)
    end

    def create
        if !verify_recaptcha
            flash.delete :recaptcha_error
            build_resource(sign_up_params)
            resource.valid?
            resource.errors.add(:base, "There was an error with the recaptcha code below. Please re-enter the code.")
            clean_up_passwords(resource)
            respond_with_navigational(resource) {render :new}
        else
            flash.delete :recaptcha_error
            super
        end
    end

    def after_inactive_sign_up_path_for(resource)
        '/pages/confirm_email'
    end
end

我在用户模型和控制器中有标准的设计东西,它们没有任何改变,但它停止了工作。不知道如何解决这个问题

编辑:rake 路由显示由于某种原因没有注册路径:

                               Prefix Verb     URI Pattern                                                             Controller#Action
                          rails_admin          /admin                                                                  RailsAdmin::Engine
                     new_user_session GET      /users/sign_in(.:format)                                                devise/sessions#new
                         user_session POST     /users/sign_in(.:format)                                                devise/sessions#create
                 destroy_user_session DELETE   /users/sign_out(.:format)                                               devise/sessions#destroy
user_google_oauth2_omniauth_authorize GET|POST /users/auth/google_oauth2(.:format)                                     callbacks#passthru
 user_google_oauth2_omniauth_callback GET|POST /users/auth/google_oauth2/callback(.:format)                            callbacks#google_oauth2
     user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format)                                          callbacks#passthru
      user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format)                                 callbacks#facebook
                    new_user_password GET      /users/password/new(.:format)                                           devise/passwords#new
                   edit_user_password GET      /users/password/edit(.:format)                                          devise/passwords#edit
                        user_password PATCH    /users/password(.:format)                                               devise/passwords#update
                                      PUT      /users/password(.:format)                                               devise/passwords#update
                                      POST     /users/password(.:format)                                               devise/passwords#create
                new_user_confirmation GET      /users/confirmation/new(.:format)                                       devise/confirmations#new
                    user_confirmation GET      /users/confirmation(.:format)                                           devise/confirmations#show
                                      POST     /users/confirmation(.:format)                                           devise/confirmations#create

好的,所以这个错误让我感到困惑。我正在使用 docker-compose 进行开发,并重建了容器和图像。删除所有内容并重新开始。设计的注册设置中的某些东西搞砸了,不知道在哪里看它是否正确。所以当我去:localhost:8000/users/sign_in 一切正常,它进入设计登录页面......但是当我去localhost:8000/users/sign_up 它说Couldn't find User with 'id'=sign_up。它就像 devise_for :users 被忽略了注册的东西,不知道在哪里寻找那个原因这都是 gem 内部的......我认为

【问题讨论】:

  • 使用rake routes 查看应用程序的路由。如果你的模型是用户,它应该是user_registration_path
  • 当我运行 rake 路线时,由于某种原因它不存在...编辑帖子以显示所有设计路线
  • 你安装了devise 吗?
  • 是的,它有所有其他设计路线,我重新运行安装只是为了确保
  • @DRing 尝试重新启动服务器。

标签: ruby-on-rails ruby devise


【解决方案1】:

因此,经过数小时挖掘我的代码,与我的旧代码进行比较并在谷歌上搜索我能想到的所有设计和注册组合。我偶然发现了这个问题,这很愚蠢......在编辑我的 User.rb 模型时,我不小心从设计模块中删除了 :registable 。这就是它没有生成注册路由的原因。所以从这里修复了我的代码行:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable,  :confirmable, 
       :recoverable, :rememberable, :trackable, :validatable,
       :omniauthable, omniauth_providers: [:google_oauth2, :facebook]

到这个:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :confirmable, 
       :recoverable, :rememberable, :trackable, :validatable,
       :omniauthable, omniauth_providers: [:google_oauth2, :facebook]

不确定在何处或如何删除,但确实删除了,因为它位于块的中间,当我查看我的代码时没有注意到

【讨论】:

    【解决方案2】:

    同时尝试重新启动服务器。

    我的用户控制器中有:registrable,但一直收到同样的错误。重新启动服务器帮助了我。

    【讨论】:

    • 谢谢。生成用户模型和路由后我没有重启服务器
    • 不用担心,很高兴它有帮助:)
    【解决方案3】:

    我几乎花了一天的时间来研究这个并发现了一个愚蠢的错误。在我的设计模型(即艺术家)中做一些其他事情时,我错误地从代码中删除了 registerable

    修改了代码

    devise :database_authenticatable,
             :recoverable, :rememberable, :validatable
    

    devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
    

    【讨论】:

      【解决方案4】:

      删除目录 /devise。然后重新运行 rails generate devise:view 这对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-31
        • 2011-11-19
        • 2014-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多