以范围自定义形式呈现设计错误消息 [英] Rendering devise error messages in scoped custom form
问题描述
我必须设计模型, User
和 Vendor
。我为供应商生成了视图,并根据自己的需要定制了注册表单。我需要调整Vendor的注册控制器,像这样:
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
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
我对Vendor的注册视图如下:
My signup view for Vendor looks like so:
views / vendors / registrations / new.html.erb
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:
也许您可以尝试使用标准的Rails错误显示替换 devise_error_messages!
:
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 %>
这篇关于以范围自定义形式呈现设计错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!