设计错误不起作用?

时间:2014-03-04 00:00:10

标签: ruby-on-rails devise

我正在使用Devise 3.2.3和Rails 4.1.0.rc1,我只是想在出现错误登录时出现错误。

以下是表格:

  

<div class="row">
  <div class="login-box">
    <h2>Sign in</h2>

    <%= form_for resource, :as => resource_name, :url => session_path(resource_name), html: { class: "form-horizontal" } do |f| %>
      <%= devise_error_messages! %>

      <fieldset>
        <%= f.email_field :email, class: "input-large col-xs-12", placeholder: "Email", :autofocus => true %>

        <%= f.password_field :password, class: "input-large col-xs-12", placeholder: "Password" %>

        <div class="clearfix"></div>
        <div class="field-spacer"></div>

        <%#= f.password_field :password_confirmation, class: "input-large col-xs-12", placeholder: "Confirm Password" %>

        <%= f.check_box :remember_me %><%= f.label :remember_me, "Remember Me?" %>

        <div class="field-spacer"></div>

        <%= f.submit "Sign in", class: "btn btn-primary col-xs-12" %>

        <div class="clearfix"></div>

        <div class="text-center margin-top-10"><a href="<%= new_user_registration_path %>" class="underline">No account? Sign up here.</a></div>

        <div class="clearfix"></div>
        <div class="field-spacer"></div>

      </fieldset>
    <% end %>

    <hr>
    <h3>Forgot Password?</h3>
    <p>
      No problem, <a href="<%= new_user_password_path %>" class="underline">click here</a> to get a new password.
    </p>  
  </div>
</div><!--/row-->

  </div><!--/row-->   

这是追踪:

Started POST "/users/sign_in" for 127.0.0.1 at 2014-03-03 17:43:50 -0600
Processing by Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"QuDKO+mP/iupA5k3ofeBO4Hr3mvFNZmyZooaBxNE0K4=", "user"=>{"email"=>"fsfa@gasgsa.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
  User Load (1.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."email" = 'fsfa@gasgsa.com'  ORDER BY "users"."id" ASC LIMIT 1
Completed 401 Unauthorized in 7ms
Processing by Devise::SessionsController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"QuDKO+mP/iupA5k3ofeBO4Hr3mvFNZmyZooaBxNE0K4=", "user"=>{"email"=>"fsfa@gasgsa.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
  Rendered devise/sessions/new.html.erb within layouts/application (1.1ms)
  Rendered shared/_head.html.erb (1.2ms)
  Rendered shared/_errors.html.erb (0.0ms)
  Rendered shared/_header.html.erb (0.6ms)
  Rendered shared/_footer.html.erb (0.1ms)
Completed 200 OK in 69ms (Views: 8.8ms | ActiveRecord: 0.0ms)

知道为什么这不会将错误返回给视图吗?

更新1:

我把这个文件放在:

module DeviseHelper
  def devise_error_messages!
    debugger
    return '' if resource.errors.empty?

    messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
    html = <<-HTML
    <div class="alert alert-error alert-block"> <button type="button"
    class="close" data-dismiss="alert">x</button>
      #{messages}
    </div>
    HTML

    html.html_safe
  end
end

我使用调试器来查看发生了什么。当我点击提交时,电子邮件或密码字段中没有信息(应该设置设计错误),该帮助方法中的resource没有任何错误。但是,在调试器控制台中,如果我运行resource.valid?,然后让它完成运行,它会正确地创建错误。这有什么问题?

1 个答案:

答案 0 :(得分:0)

将设计错误消息添加到闪存中,只要您的application.html.erb文件包含它们就应该显示,您可以阅读Ryan Bates优秀railscast中的详细信息

http://railscasts.com/episodes/209-devise-revised?view=asciicast

另请看这篇文章 Devise not displaying error messge during an authentication failure?