在身份验证失败期间设计不显示错误消息?

时间:2010-11-06 05:46:54

标签: ruby-on-rails ruby ruby-on-rails-3 authentication

我希望在设计中发生身份验证失败时发出闪存通知。但在身份验证失败期间什么也得不到,只是页面刷新并保持静止。 我没有改变任何东西。默认设计安装本身不显示具有无效身份验证尝试的闪存错误。我刚刚安装了一个设计作为宝石的设计。甚至不能改变生成的代码 可能是因为某些浏览器的可比性问题 但我得到其他手动介绍闪光消息其他工作。

关于可能被打破的任何建议。

我正在使用rails 3.0.1
*更新*

我收到用户注册(注册)的失败消息,但没有登录失败消息。 一些谷歌关于这个话题透露,对于注册,它期望: -   <%= devise_error_messages! %GT; 但是对于登录,它需要引用一些其他警报消息标记,但是没有得到我必须使用的警报标记的确切信息并且可以使用???

请提供一些建议!!!
提前谢谢。

4 个答案:

答案 0 :(得分:37)

经过一番大量的搜索/浏览后,我找到了答案,
你必须在我们的application.html.erb文件中添加以下代码

  <%- flash.each do |name, msg| -%>
    <%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
  <%- end -%>  

添加此内容之后,我能够看到sign_in失败警报消息:)。

答案 1 :(得分:2)

不可否认,有点hacky,但我正在使用这个助手(app / helpers / devise_helper.rb)来获取闪存并使用那些设置然后默认为resource.errors。这只是基于设计库中的帮助程序。

module DeviseHelper

  def devise_error_messages!
    flash_alerts = []
    error_key = 'errors.messages.not_saved'

    if !flash.empty?
      flash_alerts.push(flash[:error]) if flash[:error]
      flash_alerts.push(flash[:alert]) if flash[:alert]
      flash_alerts.push(flash[:notice]) if flash[:notice]
      error_key = 'devise.failure.invalid'
    end

    return "" if resource.errors.empty? && flash_alerts.empty?
    errors = resource.errors.empty? ? flash_alerts : resource.errors.full_messages

    messages = errors.map { |msg| content_tag(:li, msg) }.join
    sentence = I18n.t(error_key, :count    => errors.count,
                                 :resource => resource.class.model_name.human.downcase)

    html = <<-HTML
    <div id="error_explanation">
      <h2>#{sentence}</h2>
      <ul>#{messages}</ul>
    </div>
    HTML

    html.html_safe
  end

end

答案 2 :(得分:0)

我知道这已经过时了,但我最终在这里因为我在rails 5.1中遇到了同样的问题并且接受的答案没有奏效,所以这就是我的所作所为。重写Devise :: SessionController后,将以下代码添加到其中:

after_action :unauthenticated

protected

def unauthenticated
  flash[:alert] = t("devise.failure.#{request.env['warden'].message}") unless request.env['warden'].message.blank?
end

此外,在同一个控制器上,从您的Devise版本中复制并粘贴create方法的代码,然后从!中删除warden.authenticate!。因为您删除了!,所以现在必须检查资源是否为nil,如果是,则重定向。在我的例子中,create方法最终如下:

def create
  self.resource = warden.authenticate(auth_options)
  redirect_to root_path and return if resource.nil?
  set_flash_message!(:notice, :signed_in)
  sign_in(resource_name, resource)
  yield resource if block_given?
  respond_with resource, location: after_sign_in_path_for(resource)
end

最后,您只需在视图上打印Flash消息即可。我正在使用materialize,所以我创建了一个部分并向其添加了以下代码(您应该根据自己的需要进行自定义):

<% flash.each do |type, message| %>
  <% if type == "notice" %>
    <script id="toast">
      $(function() {
        Materialize.toast('<%= message %>', 4000);
      });
    </script>
  <% elsif type == "success" %>
    <script id="toast">
      $(function() {
        Materialize.toast('<%= message %>', 4000, 'green');
      });
    </script>
  <% elsif type == "alert" %>
    <script id="toast">
      $(function() {
        Materialize.toast('<%= message %>', 4000, 'orange');
      });
    </script>
  <% end %>
<% end %>

答案 3 :(得分:0)

Rails 5.1,Devise 4.4.3

在表单内,错误可以通过以下方式显示:

<% resource.errors.full_messages.each do |msg| %>
    <%= msg %>
<% end %>
相关问题