【发布时间】:2012-09-30 22:04:00
【问题描述】:
设计错误消息!看原因
undefined method 'errors' for nil:NilClass
在create 方法中的render :new 之后。这开始发生在我从“Devise::RegistrationsController”而不是“ApplicationController”继承 RegistrationsController 之后。 “新”方法的初始渲染不会导致任何异常。
重写的注册控制器:
class RegistrationsController < Devise::RegistrationsController
def create
begin
raise I18n.t("registration_disabled") unless registration_enabled?
....................
rescue => ex
flash[:alert] = ex.message
render :new
end
end
end
视图registrations/new.html.erb:
<h2><%= I18n.t("sign_up_title") %></h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :login, I18n.t("the_login") %> <span class="mandatory">*</span><br />
<%= f.text_field :login %></div>
<div><%= f.label :password, I18n.t("password") %> <span class="mandatory">*</span><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation, I18n.t("password_confirmation") %> <span class="mandatory">*</span><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit from_admin? ? I18n.t("sign_up_other") : I18n.t("sign_up") %></div>
<p class="mandatory">* - <%= I18n.t("mandatory_fields") %></p>
<% end %>
<%= render "devise/links" %>
【问题讨论】:
标签: ruby-on-rails exception devise registration