Authlogic_OAuth fails with error "uninitialized constant UserSession::OAuth" in Rails 3
I've gotten my application to work (i.e. sign_in and sign_up) with Authlogic and I'm now trying to add support for OAuth through the Authlogic_OAuth gem. I've gotten all of the basics set up (I think) and I've added a "Login with Twitter" button to my landing page. The problem is that when I click the button I get this error:
uninitialized constant UserSession::OAuth
with the application trace:
app/models/user_session.rb:17:in `oauth_consumer'
app/controllers/user_sessions_controller.rb:23:in `create'
The function that is failing is in my user_session model:
# authlogic_oauth hacks 开发者_开发问答
# Twitter example
def self.oauth_consumer
OAuth::Consumer.new("TOKEN", "SECRET",
{ :site => "http://twitter.com",
:authorize_url => "http://twitter.com/oauth/authenticate"})
end
I'm pretty new to rails and ruby so I don't quite understand where this namespace collision is coming from or how to solve it. Any help would be greatly appreciated.
OK, I figured it out. The problem is that OAuth::Consumer is not defined withing authlogic_oauth. It's actually defined in the oauth gem. so I updated my Gemfile to:
gem 'authlogic', '2.1.6'
# set up oauth capabilities. Note: :lib is replaced with :require in rails 3
gem 'authlogic-oauth', '1.0.8', :require => 'authlogic_oauth'
gem 'oauth', '0.4.4'
then run:
bundle install
and, don't forget to restart the rails server so that it reloads the gems from the Gemfile.
Try to change OAuth
to ::OAuth
. The colons mean that you want to access OAuth
from outside your class. It was searching from your class.
精彩评论