Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Upgrading to Rails 3: Undefined method `key' for #<UserSession: no credentials provided> #101

Closed
simonwh opened this Issue Feb 20, 2010 · 28 comments

Comments

Projects
None yet

simonwh commented Feb 20, 2010

I've been upgrading one of my apps to Rails 3 today and have run into some issues with AuthLogic. I've heard that it should work with Rails 3?

When trying to access the login view (/user_session/new)

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

Where line 3 is the form_for

3: <% form_for @user_session, :url => user_session_path do |f| %>

Any clues? I'm thinking in the lines of it not recognizing the user model or alike, it does have the act_as_authentic as always though.

I have the same problem, except it throws

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

Adding a custom to_key to UserSession
def to_key
id ? id : nil
end

will solve the undefined method error, but the form screws up. The form action contains a wrong path. It should target /user_session (or the route you set up), but it targets
/user_sessions/%23<UserSession: ####>

I have no clue how to fix that, but i'll investigate further. =)

simonwh commented Feb 23, 2010

I just took a look at this again, and now I'm getting the to_key error method error aswell.

I tried creating an empty to_key method in the UserSession model and now everything works. Where do Authlogic use the to_key method and for what?

@russbuelt My form action path is good ("/user_session"). What does your form look like?

@simonwh
Thats really wired. I just setup a sandbox app with authlogic and the only error I got was
undefined method 'persisted?' for #...
to get rid of that i just added the persisted? method. Everything works fine.

Oh yes the only difference: I have not added any activations or password resets to the sandbox app. AND i have not used Subdomains/accounts.

My form:
http://gist.github.com/312592

simonwh commented Feb 23, 2010

@russbuelt Does sounds wierd. I don't know a lot about authlogic so can't really tell why this is happening and why it need methods that doesn't really do anything (or so it seems).

I guess your form builder line looks like the one I posted in my first message. I find it weird that it tries to link to the UserSession object aswell?

There are some changes in the primary key handling in ActiveModel. They changed key to to_key.
def to_key
new_record? ? nil : [ self.send(self.class.primary_key) ]
end

Adding this to your user_session model should completely resolve those key errors. =)

Have a look at:
http://github.com/rails/rails/commits/master/activerecord/lib/active_record/attribute_methods/primary_key.rb

simonwh commented Feb 25, 2010

@russbuelt That's it, good find. How do we notify the authors of this? (I'm new to github).

@simonwh Thats pretty simple.

I guess Ben is reading all those issues, and is already aware of this change. If you want to, you could simply visit his profile and send him a Message or mention his GitHub username (@username) an he will be notified.

simonwh commented Feb 25, 2010

@russbuelt Great. Alright, it's all in your hands now @binarylogic. So do I close this issue? Since it has been solved for us, but not in the repository.

@simonwh leave it open for now. Maybe someone could provide a patch at some time. This would be a good place to post it =)

namxam commented Feb 26, 2010

Or just fork the project, fix the bug and send a pull request. The steps are explained in the github help section.

voxxit commented Jul 18, 2010

+1. This happens for me, too. I fixed it in my fork, and sent a pull request. Hopefully it will be fixed soon!

Eh, I installed 2.1.6 and rails 3 rc and I still get this. I'm sending a PM to binarylogic in the hopes that he sees it, considering he's released a new version since voxxit sent a pull request and hasn't incorporated the fix.

comron commented Aug 18, 2010

Seems that even with voxxit's branch when I generate a form from a UserSession object the path is "/user_sessions/%23%3CUserSession:0x104735d70%3E"

I'm also receiving the /user_sessions/%23%3CUserSession ...." error after incorporating the to_key method into the user_sessions model.
Has any progress been made on this front?

heronog commented Aug 20, 2010

@naderhen @comron change your

<% form_for(@user_session) do |f| %>

to

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

comron commented Aug 20, 2010

@heronog Don't know how I overlooked that, but that fixes it right up.

deised commented Oct 20, 2010

Fixed it for me, I was getting this error from a generated resources view in Rails 3:

ActionView::Template::Error (undefined method `to_key' for #<UserSession: no credentials provided>):
1: <%= form_for(@user_session) do |f| %>
2: <% if @user_session.errors.any? %>
3:


4:

<%= pluralize(@user_session.errors.count, "error") %> prohibited this user_session from being saved:


app/views/user_sessions/_form.html.erb:1...
app/views/user_sessions/new.html.erb:3:...

I removed the gem and installed this plugin via "rails plugin install git://github....." and now all is fine.

Many thanks!

Mange commented Oct 22, 2010

When will this fix get distributed as a new gem version?

I've been upgrading my app to Rails 3 this weekend and none of the above fixes work. The only fix I found was adding the following to my UserSession model:

http://mrjaba.posterous.com/rails-3-active-model-undefined-method-tokey-f
include ActiveModel::Conversion
def persisted?
false
end

However, I'm not sure if this will cause issues down the road.

I echo Mange's comment. Very confused about how this is being handled. It's annoying that the patch has existed for months and only recently got merged into master, but even now there is no gem version. It's just a minor annoyance that seems to indicate a lack of maintenance which is frustrating.

Emerson commented Nov 21, 2010

Would be nice to see the gem updated with the latest code in master. Sounds like a lot of people depend on this gem!

For those of you who are not afraid of living on the edge, you can force your rails app to use the latest code on github by adding the following code to your Gemfile:

gem 'authlogic', :git => 'git://github.com/binarylogic/authlogic.git'

After a quick 'bundle install', the problem seems fixed for me.

@Emerson
My rails 3 app also seems to work fine by using the latest code off github.
Thanks!

tvdeyen commented Dec 1, 2010

I also can confirm that latest code of github fixes this. Gem on rubygems.org seams to be outdated. So Ben could you please update this? Thanks!

phlegx commented Jan 25, 2011

I also confirm this error!

haslo commented Feb 10, 2011

I just came back to check whether we could remove the fix from our application and use the newest gem instead ... the changes haven't propagated to the gems repositories yet, right?

Cloning this repo, building from the gemspec, and installing the built gem worked fine for me.

Collaborator

tiegz commented Aug 20, 2014

The gems should be in a better state now. Closing this now, let us know if anyone still gets it on the latest versions.

@tiegz tiegz closed this Aug 20, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment