Search code examples
ruby-on-railsdevise

Devise - Invalid Email or Password with valid information


I'm encountering an interesting issue when signing in/out users with devise. 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

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 (where the sign-in, sign-out link is)

<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 %>

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


Solution

  • The parameter names in your session view are wrong. Both are named signup. The controller won't be able to identify them.

    Supposing you have the default Devise::SessionsController, they should look like:

    <%= 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].