ruby-on-rails - Rails Devise 注册表单未定义方法 `devise_error_messages!'

标签 ruby-on-rails ruby devise

我正在尝试使用设计来处理用户登录/注册/忘记密码

我想在我的根 url root 'home#index' 处有注册表单

家庭 Controller

class HomeController < ApplicationController

    include DeviseHelper

    def index

    end

end

设备助手

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

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>

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

undefined method `devise_error_messages!' for #<#:0x007fb430638300>

我该如何解决这个错误。使用 Devise 模块也是一种好的做法吗?

最佳答案

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

与引入您自己的实现相比,这样做的优势在于 do devise_error_messages!是您将自动获取 Devise 在未来版本中可能做出的方法更改,并且您的应用程序中的代码会更少,这两者都会使您的代码更合理/更易于维护。

关于ruby-on-rails - Rails Devise 注册表单未定义方法 `devise_error_messages!',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32659270/

相关文章:

ruby-on-rails - Rails 路由命名空间错误?

ruby-on-rails - Rails Devise + Devise-bascamper 如何让 super 用户登录任意账户?

ruby-on-rails - 是否可以将 Blocks 或 Procs 作为参数发送给 Sidekiq?

ruby-on-rails - 是否可以使用十六进制或 RGB 颜色设置行样式或在电子表格 gem 中使用偶数/奇数属性?

java - AWS DynamoDB 选择日期范围的查询

ruby-on-rails - 将 i18n gem 与部分模板文件一起使用时出现问题

html - nokogiri 从选定的 div 中抓取所有子 div

ruby - 我怎样才能做连续的破坏性加法/减法?

ruby-on-rails - Heroku 暂存环境未部署所有图像和 CSS

ruby-on-rails-3 - 使用 Devise 计算每日活跃用户