简体   繁体   English

RoR设计:sign_in始终返回无效的电子邮件/密码

[英]RoR devise: sign_in always returns invalid email/password

Every time I log in, I get the error msg that the email/password is invalid. 每次登录时,都会收到错误信息,表明电子邮件/密码无效。

routes: 路线:

devise_for :users

devise_scope :users do
  get '/users/sign_out' => 'devise/sessions#destroy', :as => :destroy_user_session
  post '/users/sign_in' => 'devise/sessions#create', :as => :user_session
end

resources :users

user model: 用户模型:

devise :database_authenticatable, :confirmable, :recoverable, :rememberable, :trackable

attr_accessor :password
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :gender, :remember_me

view: 视图:

<% if signed_in?(:user) %>
   Hi <%= current_user.first_name %>. | Not you? <%= link_to 'Sign out', destroy_user_session_path, :method => :delete %>
<% else %>
   <%= link_to 'Sign up', signup_path %> or <%= link_to 'Sign in', user_session_path, :method => :create %>
<% end %>

I tried changing the routes to: 我尝试将路线更改为:

get '/users/sign_in' => 'devise/sessions#new', :as => :new_user_session

and changing the respective paths, but that didn't change anything. 并改变各自的路径,但这并没有改变任何东西。

I even changed the code in the view from: 我甚至改变了视图中的代码:

if signed_in?(:user)

to: 至:

if user_signed_in?

and did a combination of these things and nothing is working. 并做了这些事情的组合,没有任何工作。

I did ask devise to handle the confirmation as well, which I did by copying the generated link to the browser and it signs me in as the user the first time. 我确实要求设计处理确认,我通过将生成的链接复制到浏览器并将其作为用户第一次签名来完成。 It also allows me to change the password via the email confirmation link, which also signs me in upon changing the password. 它还允许我通过电子邮件确认链接更改密码,该链接也会在更改密码时签署。 But once I sign out and sign back in, it tells me that the email/password is invalid again. 但是一旦我退出并重新登录,它就会告诉我电子邮件/密码再次无效。

Can anyone help? 有人可以帮忙吗?

I am using rails 3.0.7, devise 1.4.5, capybara 1.1.1, cucumber 1.0.6, mysql2 0.2.6 and rake 0.8.7 if that helps anyone. 我正在使用rails 3.0.7,设计1.4.5,capybara 1.1.1,黄瓜1.0.6,mysql2 0.2.6和rake 0.8.7,如果这有助于任何人。

Thanks 谢谢

EDIT: 编辑:

To help future users, there is actually nothing wrong with the gem. 为了帮助未来的用户,宝石实际上没有任何问题。 It works fine. 它工作正常。 The issue is with my database. 问题出在我的数据库中。 For some reason, it is selecting a NULL email from the database instead of pulling the info of the user I am trying to log in. I am figuring out how to fix this now and will update once I figure it out. 出于某种原因,它正在从数据库中选择一个NULL电子邮件,而不是拉动我尝试登录的用户的信息。我正在弄清楚如何解决这个问题,并且一旦我搞清楚就会更新。

I have been having a similar issue on a fresh install of Devise, but what I found is that if I ran bundle update then restarted my dev server it works. 我在Devise的全新安装上遇到了类似的问题,但我发现如果我运行bundle update那么重新启动我的dev服务器就行了。

At first I wasn't seeing the DB query in the development.log , but once I updated my Gemfile (to make sure I am using the latest Devise gem) and restarted the server I now see the db queries in my logfile and it works (magically). 起初我没有在development.log看到数据库查询,但是一旦我更新了我的Gemfile (以确保我使用的是最新的Devise gem)并重新启动服务器,我现在在我的日志文件中看到db查询并且它可以工作(神奇)。

Hope that helps. 希望有所帮助。

Btw, not sure if this will help you...but these are the routes in my Routes.rb 顺便说一句,不知道这对你有帮助......但这些是我的Routes.rb中的路线

 devise_for :users, :path_names => { :sign_up => "register", 
                                      :sign_in => "login", 
                                      :sign_out => "logout",
                                      :settings => "settings" }

  devise_scope :user do
    get "login", :to => "devise/sessions#new"
    get "register", :to => "devise/registrations#new"
    get "settings", :to => "devise/registrations#edit"
    get "logout",   :to => "devise/sessions#destroy"

  end

I ran into this issue when I had both 当我同时遇到这个时,我遇到了这个问题

config.authentication_keys = [ :username ]

and

config.authentication_keys = [ :email ]

enabled simultaneously in config/initializers/devise.rb. 在config / initializers / devise.rb中同时启用。


Upon removing the duplicate setting, authentication was successful. 删除重复设置后,身份验证成功。

When I ran across this problem I went back through my Devise config file in 当我遇到这个问题时,我通过我的Devise配置文件返回

config/initializers/devise.rb 配置/初始化/ devise.rb

I then found the part that I remembered I had changed to :username 然后我找到了我记得我改变的部分:用户名

config.authentication_keys = [ :username ]

I changed this back to :email . 我把它改回来了:email That ended up making it work. 最终使它成功。

I think the attr_accessor messes up with attr_accessible . 我认为attr_accessorattr_accessible混淆了 Do you really need to have an attr_accessor?, if not simply remove it. 你真的需要一个attr_accessor吗?如果不是简单的删除它。

Try it and please let me know how it goes. 试试吧,请告诉我它是怎么回事。

Best regards 最好的祝福

ED ED

config.authentication_keys = [ :username ] config.authentication_keys = [:用户名]

and

config.authentication_keys = [ :email ] config.authentication_keys = [:email]

simultaneously set in config/initializers/devise.rb will cause this error. 同时在config / initializers / devise.rb中设置将导致此错误。

The username and password could both be case sensitive. 用户名和密码都可以区分大小写。 Have you checked how the username is stored in your table to make sure you are entering it correctly? 您是否检查过用户名如何存储在表中以确保正确输入?

In my experience when it tells you the email/password is incorrect, you can get distracted with lots of complicated ideas, but it ends up being the email or password was incorrect. 根据我的经验,当它告诉您电子邮件/密码不正确时,您可能会分心注意许多复杂的想法,但最终导致电子邮件或密码不正确。 It sounds like you have tried various times with resetting the password, so it makes me wonder about the username. 听起来你已经尝试过多次重置密码,所以这让我想知道用户名。 Are you sure the right field is set up in devise as the username (ie. it could be email or a username)? 您确定在设计中将正确的字段设置为用户名(即,它可以是电子邮件还是用户名)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM