Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 4.2 : ActionView::Template::Error (undefined method `model_name' for #<UserSession: no credentials provided>) #432

Closed
aurels opened this issue Sep 22, 2014 · 21 comments

Comments

@aurels
Copy link

aurels commented Sep 22, 2014

Hello,

I'm migrating my app from Rails 4.1 to 4.2, with AuthLogic version 3.4.2.

When visiting the login page, I get this error in the view :

undefined method `model_name' for #<UserSession: no credentials provided>

Here is the associated controller :

class UserSessionsController < ApplicationController

  layout 'login'

  before_action :login_required,    :only => [:destroy]
  before_action :no_login_required, :only => [:new, :create]

  def new
    @user_session = UserSession.new
  end

  ...
end

And the session model is empty :

# coding: UTF-8

class UserSession < Authlogic::Session::Base
end

And the user model :

acts_as_authentic do |config|
    config.maintain_sessions = true
    config.merge_validates_length_of_password_field_options({ :minimum => 8 })
end

Any idea ? Thanks.

@codeodor
Copy link

I thought Rails went to requiring ActiveModel::Naming-compliant objects for forms and links a long time ago, but I guess not because I'm getting the same error.

Until a proper fix is released in AuthLogic, you can use this, which seems to have worked for me:

class UserSession < Authlogic::Session::Base
  include ActiveModel::Naming
  def parents
    []
  end

  def name
    "UserSession"
  end
end

Caveat: I still have a bunch of errors to work through, but this removed the error on the login form for my app. No idea if logging in actually works because other errors are preventing the pages from loading. 😄

@codeodor
Copy link

So I'm not sure if that's all that is needed. Trying to log in now, and it seems like none of my passwords are matching. But I'm updating from an app that started life in Rails 1.x, and its latest version is being updated from 3.2 to 4.2.

@aurels
Copy link
Author

aurels commented Oct 1, 2014

Hi @codeodor,

I'll try that ASAP and tell if I have other issues.

It's an old app we have been migrating too. From Rails 2.0 to 2.2, then to 3.0, then to 3.2, then to 4. And now to 4.2.

@aurels
Copy link
Author

aurels commented Oct 1, 2014

Yep it does fix the login page and the passwords are matching. I did not test the other features yet, I'll let you know if Authlogic fully works on 4.2 for me.

Thanks again !

bwlang added a commit to bwlang/authlogic that referenced this issue Oct 8, 2014
@codeodor
Copy link

codeodor commented Oct 9, 2014

@bwlang I think class.name might be a better general purpose name for use inside Authlogic, since people can name their implemented classes other than UserSession

(mentioned in case you're making that a pull request)

bwlang added a commit to bwlang/authlogic that referenced this issue Oct 9, 2014
@konung
Copy link

konung commented Oct 13, 2014

That fixed the same problem for me on an app that is being upgraded from 3.2 to 4.2

@tiegz
Copy link
Collaborator

tiegz commented Oct 15, 2014

@aurels @codeodor curious if you could try another solution too... does it work with default Authlogic if you set config.eager_load = true in config/environments/development.rb?

@tiegz
Copy link
Collaborator

tiegz commented Oct 15, 2014

( @codeodor the password matching thing could be because the default crypto provider changed to SCrypt. I might try to make the messaging more obvious in README: 33dca9e)

@bwlang
Copy link

bwlang commented Oct 16, 2014

I'm running the 4.2 code suggested by codeodor in production now... seems solid.
I have not run the test suite yet, but once I do I'll issue a pull request.

@codeodor
Copy link

@tiegz My tests were giving errors, so I enabled eager_load in that environment and removed my patch on UserSession. Unfortunately, the error persisted, so setting config.eager_load to true does not seem to fix the issue.

@aurels
Copy link
Author

aurels commented Oct 16, 2014

@tiegz No it doesn't work with in config/environments/development.rb?, and I don't see why it would ;-)

@codeodor
Copy link

@tiegz Thanks for the tip about the password matching. I enabled migration and it worked like a charm!

@orien
Copy link

orien commented Nov 11, 2014

This fixed the problem for me:

class UserSession < Authlogic::Session::Base
  extend ActiveModel::Naming
end

@codeodor
Copy link

@orien Of course! Why didn't I think of that? 😁 That's a much better solution.

@cpg
Copy link

cpg commented Dec 20, 2014

With the Rails 4.2 release out, this is starting to hit more and more authlogic-based apps.

Is what @orien suggested a safe work-around? (seems to be working well for my app)

@tiegz
Copy link
Collaborator

tiegz commented Dec 21, 2014

I've just pushed a commit to fix this (and cleanup some old methods)... wfm in a new Rails 4.2 app. If anyone else can confirm, I'll update the gem too:

0e66c86

@codeodor
Copy link

@cpg Yes, that solution is safe and works. However, the update to this repo from @tiegz is better, since it's going to make it into the gem. :)

@tiegz I can confirm that commit fixes the issue I was seeing here.

@cpg
Copy link

cpg commented Dec 21, 2014

@tiegz I can also confirm that the pushed changes [1]appear to work well after removing the extend work-around above from my app.

[1] i.e. deploying from git@github.com:binarylogic/authlogic.git

@tiegz
Copy link
Collaborator

tiegz commented Dec 23, 2014

Ok, thanks everyone! Just pushed the v3.4.4 gem:

https://github.com/binarylogic/authlogic/tree/v3.4.4

@tiegz tiegz closed this as completed Dec 23, 2014
@codeodor
Copy link

Awesome, thanks! 👍

@cpg
Copy link

cpg commented Dec 24, 2014

Did a deployment wit the official gem and basic login/logout seems to work fine. Thanks and great job.

jlduran added a commit to Labtec/OpenLIS that referenced this issue Jan 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants