繁体   English   中英

以范围自定义形式呈现设计错误消息

[英]Rendering devise error messages in scoped custom form

我必须设计模型, UserVendor 我为供应商生成了视图,并根据自己的需要定制了注册表单。 我需要调整供应商的注册控制器,如下所示:

控制器/供应商/registrations_controller.rb

class Vendors::RegistrationsController < Devise::RegistrationsController
    skip_before_filter :require_no_authentication
    def create
        super
    end

    protected

    def sign_up(resource_name, resource)
        true
    end

    def new
        super
    end
end

我的供应商注册视图如下所示:

视图/供应商/注册/new.html.erb

<h2>Sign up</h2>

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

    <div><%= f.label :store_name %>
    <%= f.text_field :store_name %></div>

      <div><%= f.label :contact_name %>
    <%= f.text_field :contact_name%></div>

      <div><%= f.label :contact_phone %>
    <%= f.text_field :contact_phone %></div>

      <div><%= f.label :address %>
    <%= f.text_field :address %></div>

      <div><%= f.label :image %>
    <%= f.file_field :image %></div>

  <div><%= f.label :email %>
  <%= f.email_field :email, :autofocus => true %></div>

  <div><%= f.label :password %>
  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %>
  <%= f.password_field :password_confirmation %></div>

  <div><%= f.submit "Sign up" %></div>
<% end %>

<%= render "vendors/shared/links" %>

当我尝试呈现页面时,出现以下错误:

undefined method `errors' for nil:NilClass

在这种情况下,如何呈现设计错误消息? 提前致谢!

我会将其写为评论,但将其作为答案更容易阅读:

也许您可以尝试替换devise_error_messages! 标准的Rails错误显示:

<% if resource.errors.any? %>
    <div class="error">
        <strong><%= pluralize(resource.errors.count, "Error") %></strong>
        <ul>
            <% resource.errors.each do |attr,msg| %>
                <li><%= msg %></li>
            <% end %>
        </ul>
    </div>
<% end %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM