[英]Devise - Invalid Email or Password with valid information
I'm encountering an interesting issue when signing in/out users with devise. 使用dev登录/注销用户时遇到一个有趣的问题。 I can sign up users just fine, but once I sign out for the first time, I can no longer sign back in (even though I'm using the same email/password combination I just signed up with). 我可以很好地注册用户,但是第一次退出后,就无法再登录(即使我使用的是刚刚注册的电子邮件/密码组合)。 It gives me an invalid email or password error. 它给我一个无效的电子邮件或密码错误。 Has anyone ever experienced this and solved it? 有没有人经历过并解决了这个问题?
Here's some code: 这是一些代码:
user.rb user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
end
routes.rb routes.rb
ChsInventory::Application.routes.draw do
devise_for :users
resources :items
resources :subjects do
resources :items_in_stock
end
root to: 'items#index'
end
_nav.html.erb (where the sign-in, sign-out link is) _nav.html.erb(登录,注销链接所在的位置)
<ul class="nav pull-right">
<% if current_user %>
<li><%= link_to 'Sign out', destroy_user_session_path, method: "delete" %></li>
<% else %>
<li><%= link_to 'Sign in', new_user_session_path %></li>
<% end %>
</ul>
_form.html.erb _form.html.erb
<%= form_tag url: '/users/sign_in', method: 'POST', :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= label_tag :signup, 'Email', :class => 'control-label' %>
<div class="controls">
<%= text_field_tag :signup, nil, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= label_tag :signup, 'Password', :class => 'control-label' %>
<div class="controls">
<%= password_field_tag :signup, nil, :class => 'password' %>
</div>
</div>
<div class="form-actions">
<%= link_to t('.cancel', :default => t("helpers.links.cancel")), root_path, :class => 'btn' %>
<%= submit_tag nil, :class => 'btn btn-primary' %>
</div>
<% end %>
Here's the output when I run rake routes: 这是我运行耙路时的输出:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
Thanks in advance 提前致谢
The parameter names in your session view are wrong. 会话视图中的参数名称错误。 Both are named signup . 两者都命名为signup 。 The controller won't be able to identify them. 控制器将无法识别它们。
Supposing you have the default Devise::SessionsController, they should look like: 假设您具有默认的Devise :: SessionsController,则它们应如下所示:
<%= form_for :user, url: '/users/sign_in', method: 'POST', :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :email, 'Email', :class => 'control-label' %>
<div class="controls">
<%= f.text_field :email, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :password, 'Password', :class => 'control-label' %>
<div class="controls">
<%= f.text_field :password, :class => 'password' %>
</div>
</div>
<div class="form-actions">
<%= link_to t('.cancel', :default => t("helpers.links.cancel")), root_path, :class => 'btn' %>
<%= f.submit '', :class => 'btn btn-primary' %>
</div>
<% end %>
This form will send the parameters as user[email] and user[password] . 该表格将以user [email]和user [password]的形式发送参数。
Devise is reading a :user
object with :email
and :password
so instead of :signup
you should use :user
. Devise正在使用:email
和:password
读取:user
对象,因此应使用:user
代替:signup
。
text_field_tag :user, …
password_field_tag :user, ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.