Sriram Chandrasekaran
2010-Oct-21  04:18 UTC
Authlogic + Rails3 - undefined method `Login'' for nil:NilClass
Im new to Rails, and decided to start of with Rails3. After a lot of
searching ive managed to get a little bit of Authlogic working. I''m
able
to register a user, login & logout.
Now, I would like to add more features, get more of authlogic working.
I''m using Railscast EP 160 as my reference.
Portions of the code found on the tutorial throw errors: Eg:
<!-- layouts/_topbar.erb -->
<%= link_to "Login", login_path %>
and I get the following error message:
undefined local variable or method `login_path'' for
#<#<Class:0x0000000311e8f8>:0x0000000310af38>
To overcome this, ive just used a string. i.e. <%= link_to "Login",
"/UserSessions/new" %>
Now it seems like i''ve reached an impasse. When i try to output the
current user with:
<%= @user.Login %>
I get an error that im unable to circumvent. Can you please help me?
Thanks :) Please find below the error message, and some of the code.
undefined method `Login'' for nil:NilClass
Full Trace Reads [truncated]
activesupport (3.0.0) lib/active_support/whiny_nil.rb:48:in
`method_missing''
app/views/layouts/_topbar.erb:16:in
`_app_views_layouts__topbar_erb__4536428193941102933_40950340__3781575178692065315''
actionpack (3.0.0) lib/action_view/template.rb:135:in `block in render''
activesupport (3.0.0) lib/active_support/notifications.rb:54:in
`instrument''
actionpack (3.0.0) lib/action_view/template.rb:127:in `render''
actionpack (3.0.0) lib/action_view/render/partials.rb:294:in
`render_partial''
actionpack (3.0.0) lib/action_view/render/partials.rb:223:in `block in
render''
activesupport (3.0.0) lib/active_support/notifications.rb:52:in `block
in instrument''
activesupport (3.0.0)
lib/active_support/notifications/instrumenter.rb:21:in `instrument''
Request Parameters:None
My gemfile reads:
gem "authlogic", :git =>
"git://github.com/odorcicd/authlogic.git",
:branch => "rails3"
config/routes.rb:
  resources :users
  resources :user_sessions
  resources :ibe
  match '':controller(/:action(/:id(.:format)))''
controllers/application_controller.rb: [the part that gets the current
user.. also taken from online examples]
  def current_user_session
    return @current_user_session if defined?(@current_user_session)
    @current_user_session = UserSession.find
  end
  def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.record
  end
models/user_session.rb:
class UserSession < Authlogic::Session::Base
include ActiveModel::Conversion
  def persisted?
    false
  end
  def to_key
    new_record? ? nil : [ self.send(self.class.primary_key) ]
  end
end
models/user.rb:
class User < ActiveRecord::Base
  acts_as_authentic
end
controllers/users_controller.rb:
class UsersController < ApplicationController
  before_filter :require_no_user, :only => [:new, :create]
  before_filter :require_user, :only => [:show, :edit, :update]
  def new
    @user = User.new
  end
  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:notice] = "Account registered!"
      redirect_back_or_default account_url
    else
      render :action => :new
    end
  end
  def show
    @user = @current_user
  end
  def edit
    @user = @current_user
  end
  def update
    @user = @current_user # makes our views "cleaner" and more
consistent
    if @user.update_attributes(params[:user])
      flash[:notice] = "Account updated!"
      redirect_to account_url
    else
      render :action => :edit
-- 
Posted via http://www.ruby-forum.com/.
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Oct-21  07:50 UTC
Re: Authlogic + Rails3 - undefined method `Login'' for nil:NilClass
On 21 October 2010 05:18, Sriram Chandrasekaran <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Im new to Rails, and decided to start of with Rails3. After a lot of > searching ive managed to get a little bit of Authlogic working. I''m able > to register a user, login & logout. > > Now, I would like to add more features, get more of authlogic working. > I''m using Railscast EP 160 as my reference. > > Portions of the code found on the tutorial throw errors: Eg: > > <!-- layouts/_topbar.erb --> > <%= link_to "Login", login_path %> > > and I get the following error message: > > undefined local variable or method `login_path'' for > #<#<Class:0x0000000311e8f8>:0x0000000310af38>You have something missing or wrong in routes.rb. I am not into Rails 3 routing yet so not sure what. Have a look at the Rails Guide on routing at http://guides.rubyonrails.org/> > To overcome this, ive just used a string. i.e. <%= link_to "Login", > "/UserSessions/new" %> > > Now it seems like i''ve reached an impasse. When i try to output the > current user with: > > <%= @user.Login %> > > I get an error that im unable to circumvent. Can you please help me? > Thanks :) Please find below the error message, and some of the code. > > undefined method `Login'' for nil:NilClassThe error is saying that you have tried to call Login on a nil object (of type NilClass, which is what nil is). The problem is therefore that @user is nil. Either you have forgotten to set it up or the code setting it has returned nil. Have you looked at the Rails Guide on debugging? Using ruby-debug you can break into your code and examine variables. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rajinder Yadav
2010-Oct-21  10:26 UTC
Re: Authlogic + Rails3 - undefined method `Login'' for nil:NilClass
On 10-10-21 12:18 AM, Sriram Chandrasekaran wrote:> Im new to Rails, and decided to start of with Rails3. After a lot of > searching ive managed to get a little bit of Authlogic working. I''m able > to register a user, login& logout.you might want to take a look at devise, its rails 3 friendly and easier to get going with, from what i read it''s suppose to be much better afik authlogic is not rails 3 ready? i had some early issues with a authlogic walk-through tutorial and abandoned it for devise -- Kind Regards, Rajinder Yadav | DevMentor.org | Do Good! ~ Share Freely GNU/Linux: 2.6.35-22-generic Kubuntu x86_64 10.10 | KDE 4.5.1 Ruby 1.9.2p0 | Rails 3.0.1 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sriram Chandrasekaran
2010-Oct-21  11:34 UTC
Re: Authlogic + Rails3 - undefined method `Login'' for nil:NilClass
> > You have something missing or wrong in routes.rb. I am not into Rails > 3 routing yet so not sure what. Have a look at the Rails Guide on > routing at http://guides.rubyonrails.org/ >Thanks Colin! Yes, I did a bit of searching online, after you suggested I take a look at routes.rb. It seems that I do have a problem with the routing part, sadly, i couldnt find a fix for the issue. I''m trying out Devise now, per Rajinder''s suggestion. Thanks again Colin & Rajinder :) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom
2010-Oct-21  18:44 UTC
Re: Authlogic + Rails3 - undefined method `Login'' for nil:NilClass
>> Im new to Rails, and decided to start of with Rails3. After a lot of >> searching ive managed to get a little bit of Authlogic working. I''m able >> to register a user, login& logout. > > you might want to take a look at devise, its rails 3 friendly and easier to get going with, from what i read it''s suppose to be much better > > afik authlogic is not rails 3 ready? i had some early issues with a authlogic walk-through tutorial and abandoned it for deviseI just set it up with Rails 3.0.1. There''s a depreciation warning about save(false) vs save(:validate => false) but otherwise it seems to be working without having to do anything special. The generator isn''t available, but it''s easy to create the user sessions class manually. Haven''t used it much, but no big explosions yet :) -philip -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.