简体   繁体   中英

Rendering devise error messages in scoped custom form

I have to Devise models, User and Vendor . I generated the views for Vendor and customized the signup form to suit my needs. I needed to tweak the registrations controller for Vendor, like so:

controllers/vendors/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

My signup view for Vendor looks like so:

views/vendors/registrations/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" %>

When I attempt to render the page, I get this error:

undefined method `errors' for nil:NilClass

How can I render devise error messages in this situation? Thanks in advance!

I would write this as a comment, but it will be easier to read as an answer:

Perhaps you could try replacing devise_error_messages! with a standard Rails error display:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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