Forum: Ruby on Rails Rails Beginner.. NoMethodError

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Af3cecc8af253b5acd3c09c5b67c0074?d=identicon&s=25 Valentino Lun (on9west)
on 2008-11-28 05:28
Dear all

I encounter error when I login my Web application
NoMethodError in AdminController#login
undefined method `get_title' for #<Class:0x63ac78>

Here is part of my code
controller/admin_controller.rb
  def login
    session[:user_id] = nil
    if request.post?
      if User.authenticate(params[:name], params[:password])
        user = User.find_by_name(params[:name])
        if !user
          user = User.new
          user.name = params[:name]
          user.title = User.get_title  #I think this trigger the error
          user.save
        end
        session[:user_id] = params[:name]
        redirect_to(:controller => 'ecpath_app_version', :action =>
'index')
      else
        #flash[:notice] = "Invalid user/password combination"
      end
    end
  end

Here is my user model
model/user.rb
require 'net/ldap'

class User < ActiveRecord::Base

  set_table_name "users"
  attr_accessor :title

  def self.authenticate(username,password)
    treebase = "dc=corp,dc=ha,dc=org,dc=hk"
    filter = Net::LDAP::Filter.eq( "sAMAccountName", username )
    login_succeeded = false
    ldap_con = initialize_ldap_con(username,password)
    if ldap_con.bind
      login_succeeded = true
      @title=ldap_con.search( :base => treebase, :filter => filter,
:attributes => 'displayname').first.displayname.to_s
    end
    login_succeeded
  end

  # I have define the get_title here..
  def get_title
    return @title
  end

My users table schema
name varchar(20)
created_at datetime
updated_at dataetime
title varchar(30)


Please give me some advices. Thank you.

Valentino
41cc2c53544016a86472343ab3fdc0ff?d=identicon&s=25 Simon Macneall (Guest)
on 2008-11-28 05:32
(Received via mailing list)
try changing User.get_title to user.get_title
you are trying to access a method on the class instead of on the
instance.

Cheers
Simon


On Fri, 28 Nov 2008 13:28:23 +0900, Valentino Lun
This topic is locked and can not be replied to.