Just another Web Developer blog

From the Blog

Nov
15

Authlogic Issues With Rails 3

Posted by Cameron on November 15th, 2010 at 10:46 am

My upgrade to Rails 3 has been surprisingly smooth,  however there have been a few minor issues with one of the Gems I use frequently, Authlogic. Both issues occur in the user_session model.

The first issue occurs when trying to load the user_sessions#new action.

NoMethodError in User_sessions#new
undefined method `key' for #<UserSession: no credentials provided>

To fix this issue, we must add the following method to the user_session model.

def to_key
  new_record? ? nil : [ self.send(self.class.primary_key) ]
end

The second issue occurs at the user_sessions#create action. The URL looks something like /user_sessions/%23…… and this obviously throws an error. The solution is to change the form tag in new.html.erb.

<% form_for (@user_session, :url => { :action => "create" }) do |f| %>
#or for formtastic
<% semantic_form_for @user_session, :url => { :action => "create" } do |f| %>

Hope this helps.

Thanks,

Cameron

  • Jonah

    Thanks Cameron! This was exactly what I needed.

  • Andrew

    the simplest solution is to include ActiveModel modules for conversion and naming:
    class UserSession < Authlogic::Session::Base
    include ActiveModel::Conversion
    extend ActiveModel::Naming

    def persisted?
    false
    end
    end

    see Railscast #219 http://railscasts.com/episodes/219-active-model