使用设计登录/注销用户时遇到一个有趣的问题。我可以很好地注册用户,但是一旦我第一次退出,我就不能再重新登录(即使我使用的电子邮件/密码组合与我刚注册时使用的相同)。它给了我一个无效的电子邮件或密码错误。有没有人经历过并解决它?
这是一些代码:
用户.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
路线.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(登录、注销链接所在的位置)
<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_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 %>
这是我运行 rake 路由时的输出:
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
提前致谢