设计"登录"表单显示"无效的电子邮件或密码"错误消息不正确

时间:2014-05-25 23:54:34

标签: ruby-on-rails ruby session error-handling devise

我正在开发一个(相对)简单的Rails应用程序。我正在使用Devise gem来处理用户会话。每当我导航到localhost:3000 / users / sign_in时,我都会看到Devise的错误消息,上面写着"无效的用户名或密码"。即使在第一次尝试登录之后也会发生这种情况,之前没有登录尝试。我试过清除我的网络浏览器的缓存无济于事。整个代码库可以在https://github.com/blahshaw/Post-Office-DNS找到,但这里有一些可能相关的代码段:

应用程序/视图/布局/ application.html.erb:

...
#section where Devise alrts are displayed
<% if notice %>
  <p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
  <p class="alert alert-danger"><%= alert %></p>
<% end %>
...

应用程序/视图/设计/会话/ new.html.erb:

<div class="container">
<h2>Sign in</h2>

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<%= f.email_field :email, autofocus: true, placeholder: "email", class: "form-control login-credentials" %><br>
<%= f.password_field :password, autocomplete: "off", placeholder: "password", class: "form-control login-credentials" %></br>
<% if devise_mapping.rememberable? -%>
    <div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
<% end -%>
<div class="btn-group">
    <%= f.submit "Sign in", class: "btn btn-primary" %>
<% end %>
<div class="btn-group">
    <%= render "devise/shared/links" %>
</div>

/config/initializers/devise.rb:

Devise.setup do |config|
  config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'
  require 'devise/orm/active_record'
  config.case_insensitive_keys = [ :email ]
  config.strip_whitespace_keys = [ :email ]
  config.skip_session_storage = [:http_auth]
  config.stretches = Rails.env.test? ? 1 : 10
  config.reconfirmable = true
  config.password_length = 8..128
  config.reset_password_within = 6.hours
  config.sign_out_via = :delete
end

如果我能提供任何信息来帮助解决问题,请告诉我。

2 个答案:

答案 0 :(得分:1)

我会更改以下内容:

-

#config/routes.rb
root 'welcome#index'

devise_for :users

resources :addresses do
    collection do
        get 'api/:key', action: :api
    end
end

resources :staticpages, only: [] do
   collection do
      get :faq
      get :api
   end
end

-

#app/layouts/application.html.erb
#remove references to devise alerts

#app/views/devise/sessions/new.html.erb
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
     <%= devise_error_messages! %>
     ...
<% end %>

你可以read about the devise_error_messages! helper here

答案 1 :(得分:0)

我通过将包含登录页面链接的:method的{​​{1}}从默认值button_to更改为:post来解决问题。我认为使用:get导致尝试使用空白凭据登录,导致显示错误消息。这是我的代码修复:

:post