设计认证 - devise_error_messages!在一个视图中导致“undefined方法”错误为nil:NilClass [英] Devise authentication - devise_error_messages! in a view causes "undefined method `errors' for nil:NilClass

查看:75
本文介绍了设计认证 - devise_error_messages!在一个视图中导致“undefined方法”错误为nil:NilClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

devise_error_messages!在一个视图中导致

 未定义的方法'n'的错误:NilClass 
$ 创建方法之后,pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $继承自Devise :: RegistrationsController而不是ApplicationController的RegistrationsController之后,就开始了。初始化新方法不会引起任何例外。



覆盖注册控制器:

  class RegistrationsController< Devise :: RegistrationsController 
def create
begin
raise I18n.t(registration_disabled)除非registration_enabled?
....................
rescue => ex
flash [:alert] = ex.message
render:new
end
end
end

视图注册/ new.html.erb:

  ; h2><%= I18n.t(sign_up_title)%>< / h2> 

<%= form_for(resource,:as => resource_name,:url => registration_path(resource_name))do | f | %GT;
<%= devise_error_messages! %GT;

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

<%= renderdevise / links%>


解决方案

我相信这是因为你提前出现异常对象(devise称它为资源)被创建。
devise_error_messages 需要它帮助者。



如果您想阻止访问注册,还有其他方法可以实现:



一种方法可以是:

  class RegistrationsController< Devise :: RegistrationsController 
def create
如果registration_enabled?
super
else
flash [:alert] = I18n.t(registration_disabled)
redirect_to action::new
end
end
end

我不是100%肯定这是否可以工作,但这个想法是渲染视图如果用户无法注册,那么这样做会像初始渲染一样。



编辑:其实我相信改变你的

 渲染动作::新

  redirect_to action::new 

将足以防止错误,因为redirect_to将执行方法,而渲染只是呈现关联视图。


devise_error_messages! in a view causes

undefined method 'errors' for nil:NilClass

after render :new in create method. This started happening after I inherited RegistrationsController from "Devise::RegistrationsController" instead of "ApplicationController". Initial rendering of "new" method does not cause any exceptions.

Overridden registrations controller:

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

The view 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" %>

解决方案

I believe it's because you're raising the exception before the object (devise calls it the resource) is created. And it is needed by the devise_error_messages helper.

If you want to prevent access to the registration, there are other ways to achieve this:

One way could be :

class RegistrationsController < Devise::RegistrationsController
  def create
    if registration_enabled?
      super
    else
      flash[:alert] = I18n.t("registration_disabled")
      redirect_to action: :new
    end
  end
end

I'm not 100% sure if this would work but this idea is to render the view with the flash if user can't register, so this would behave as the "initial rendering"

EDIT : Actually I believe that changing your

render action: :new

to

redirect_to action: :new

would be enough to prevent the error as the redirect_to will execute the methode whereas the render just render the associate view.

这篇关于设计认证 - devise_error_messages!在一个视图中导致“undefined方法”错误为nil:NilClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆