[英]Rendering devise error messages in scoped custom form
I have to Devise models, User
and Vendor
. 我必须设计模型,
User
和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 控制器/供应商/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 视图/供应商/注册/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!
也许您可以尝试替换
devise_error_messages!
with a standard Rails error display: 标准的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.