What is .....undefined method `authenticate'

def login_submit
if session[‘user’]
@logged_in = true
else
@logged_in = false
end
@user = User.new(params[‘user’])
if session[‘user’] = User.authenticate(params[‘user’][‘username’],
params[‘user’][‘password’])
flash[:notice] = l(:user_login_succeeded)
redirect_to :action => ‘list’
else
@login = params[‘user’][‘login’]
flash.now[:notice] = l(:user_login_failed)
end
end

FORM IS

Login Section

<% form_tag :controller => "user", :action => "login_submit" do %>

Username:  

Password:  


<%= submit_tag “Login”%>
<% end %>

ERROR IS

NoMethodError in UserController#login_submit

undefined method `authenticate’ for #Class:0x36f6cbc

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:1532:in
method_missing' app/controllers/user_controller.rb:34:inlogin_submit’

do you have a user model with that method defined?

On Wed, Apr 30, 2008 at 1:25 PM, Manish N.

HERE IS MY USER CONTROLLER---------

class UserController < ApplicationController

layout “home”

def list
@user = User.find(:all)
end

def new
@user = User.new
end

def create
@user = User.new(params[:user])
if @user.save
redirect_to :action => ‘list’
else
redirect_to :action => ‘new’
end
end

def login
  @user = User.new
  @user.username = params[:user][:username]
end

def login_submit
username = params[:user][:username]
password = params[:user][:password]
if session[“user”] = User.authorize(username, password)
@user = User.find(session[“user”].id)
@user.first_login = 1
@user.save
flash[:notice] = “You are now logged in”
redirect_to :action => ‘create’
else
flash[:error] = “Incorrect username and/or password”
redirect_to :action => ‘list’
end
end
end

HERE IS MY USER MODEL----

class User < ActiveRecord::Base
validates_presence_of :username, :message=>“User Name will not b
blank”
validates_presence_of :password, :message=>“Password cannot be blank”
end

You should have a method authenticate defined in User model.

In your case, authenticate method must be defined in User model as

class User < ActiveRecord::Base
validates_presence_of :username, :message=>“User Name will not
bblank”
validates_presence_of :password, :message=>“Password cannot be
blank”

# # # Authenticate method # #

def self.authenticate(login, pass)
find(:first, :conditions=>[“login = ? AND password = ?”, login,
pass]) # login, password are your users table fields…
end

end

On May 1, 9:09 am, Manish N. [email protected]

Thx prasad…

my prog is running successfully…

in future also if i have any problem i wil contact u…
can u give me u r email-id…

Thnx a lot… :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs