Rails Devise注册表单未定义的方法“ devise_error_messages!”

Jak*_*out 3 ruby ruby-on-rails devise

我正在尝试使用devise处理用户登录/注册/忘记的密码

我想在我的根URL根目录'home#index'处注册表单

家庭控制器

class HomeController < ApplicationController

    include DeviseHelper

    def index

    end

end
Run Code Online (Sandbox Code Playgroud)

设备助手

module DeviseHelper
    def resource_name
       :user
     end

     def resource_class 
        User 
     end

     def resource
       @resource ||= User.new
     end

     def devise_mapping
       @devise_mapping ||= Devise.mappings[:user]
     end

end
Run Code Online (Sandbox Code Playgroud)

views / home / index.html.erb

  <%= form_for(resource, as: resource_name, url: registration_path(resource_name))  do |f| %>

<%= devise_error_messages! %>

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

<div class="field">
  <%= f.label :password %>
  <% if @minimum_password_length %>
  <em>(<%= @minimum_password_length %> characters minimum)</em>
  <% end %><br />
  <%= f.password_field :password, autocomplete: "off" %>
</div>

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

<div class="actions">
  <%= f.submit "Sign up" %>
</div>
Run Code Online (Sandbox Code Playgroud)

当我尝试使用错误的属性进行注册时,它向我显示此错误

未定义的方法“ devise_error_messages!” 对于#<#:0x007fb430638300>

我该如何解决此错误。也是使用Devise模块的好习惯吗?

Emp*_*act 5

最好的选择是将自己的DeviseHelper重命名为其他名称。Rails的自动加载功能正在查找DeviseHelper的实现而不是Devise的实现,因此该方法未加载。称它为不太具体的名称,例如AuthHelper。

与引入自己的实现相比,这样做的优势在于devise_error_messages!就是您将自动获取对Devise在将来版本中可能采用的方法的更改,以及您的应用程序中的代码更少,这都使您的代码更加明智/可维护。