Upgrading to Rails 3: Undefined method `key' for #<UserSession: no credentials provided>
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. =)
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
@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
@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.
@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 =)
Or just fork the project, fix the bug and send a pull request. The steps are explained in the github help section.
+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.
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?
@naderhen @comron change your
<% form_for(@user_session) do |f| %>
to
<%= form_for (@user_session, :url => { :action => "create" }) do |f| %>
@heronog Don't know how I overlooked that, but that fixes it right up.
Issue seems to be resolved with commit 5661867e0ea8e572bb5b to master http://github.com/binarylogic/authlogic/commit/5661867e0ea8e572bb5b18e7afc874b3db253e3f
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:
<%= 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!
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.