使用设计登录/注销用户时遇到了一个有趣的问题。我可以很好地注册用户,但是一旦我第一次注销,我就再也无法重新登录了(即使我使用的是我刚刚注册的相同电子邮件/密码组合)。它给我一个无效的电子邮件或密码错误。有没有人经历过这个并解决了它?
以下是一些代码:
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
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
提前致谢
答案 0 :(得分:2)
会话视图中的参数名称错误。两者都命名为注册。控制器将无法识别它们。
假设你有默认的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 %>
此表单会将参数发送为用户[电子邮件] 和用户[密码] 。
答案 1 :(得分:1)
Devise正在使用:user
和:email
阅读:password
个对象,因此您应使用:signup
代替:user
。
text_field_tag :user, …
password_field_tag :user, ...