0

使用设计登录/注销用户时遇到一个有趣的问题。我可以很好地注册用户,但是一旦我第一次退出,我就不能再重新登录(即使我使用的电子邮件/密码组合与我刚注册时使用的相同)。它给了我一个无效的电子邮件或密码错误。有没有人经历过并解决它?

这是一些代码:

用户.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

提前致谢

4

2 回答 2

2

会话视图中的参数名称错误。两者都命名为signup。控制器将无法识别它们。

假设您有默认的 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 %>

此表单将参数作为user[email]user[password]发送。

于 2013-08-30T17:46:24.063 回答
1

设计正在读取一个:user对象,:email因此:password:signup应该使用:user.

text_field_tag :user, …

password_field_tag :user, ...
于 2013-08-30T17:49:51.090 回答