Search code examples
ruby-on-rails-3authlogic

Setting up Authlogic I keep hitting: "undefined method `login' for #<UserSession: no credentials provided>"


I am trying (unsuccessfully) to setup Authlogic for my Rails3 project. In the user_sessions#new view, I keep getting the following error:

undefined method `login' for #<UserSession: no credentials provided>
Extracted source (around line #7):

5:   <%= form_for(:user_session, :url => user_session_path) do |f| %>
6:     <%= f.label :login %><br>
7:     <%= f.text_field :login %><br>
8:     <%= f.password_field(:password) %>   
9:     <%= f.submit %>
10:   <% end %>

I believe that the error is coming because Authlogic is failing to detect which database column should be used for the login field.

I was under the understanding that Authlogic is supposed to fall back on the :email column if no login column is present. For belt and braces I tried adding a config option to the User class but it still did not solve the problem

Here is my code:

User model

class User < ActiveRecord::Base

  attr_accessible :password, :password_confirmation
  acts_as_authentic do |c|
    c.login_field = :email
  end
end

UserSessions controller

class UserSessionsController < ApplicationController  
  def new
    @user_session = UserSession.new
  end

  # ...
end

UserSessions#new view

<%= form_for(:user_session, :url => user_session_path) do |f| %>
  <%= f.label :login %><br>
  <%= f.text_field :login %><br>
  <%= f.password_field(:password) %>   
  <%= f.submit %>
<% end %>

UserSession model

class UserSession < Authlogic::Session::Base
   # configuration here, see documentation for sub modules of Authlogic::Session
end

DB Schema (for authlogic tables)

create_table "sessions", :force => true do |t|
  t.string   "session_id", :null => false
  t.text     "data"
  t.datetime "created_at"
  t.datetime "updated_at"
end

add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"

create_table "users", :force => true do |t|
  t.string   "email"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "first_name"
  t.string   "last_name"
  t.string   "crypted_password"
  t.string   "password_salt"
  t.string   "persistence_token"
end

Why won't Authlogic use the login column I tell it to (or is this even the problem)?


Solution

  • You're telling AuthLogic to use the email field, but then using the login field in your view instead. Change your view to:

    <%= form_for(:user_session, :url => user_session_path) do |f| %>
      <%= f.label :email %><br>
      <%= f.text_field :email %><br>
      <%= f.password_field(:password) %>   
      <%= f.submit %>
    <% end %>
    

    and it should work.