在范围自定义窗体中呈现设计错误消息

时间:2013-12-03 06:47:58

标签: ruby-on-rails devise

我必须设计模型,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

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

1 个答案:

答案 0 :(得分:-1)

我会把它写成评论,但作为答案会更容易阅读:

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

<% 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 %>