NoMethodError

I have created a simple login form…

form is like this…
<% form_tag :controller => “user”, :action => “login_submit” do %>

Username:  

Password:  


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


METHOD IN USER CONTROLLER IS LIKE THIS…

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 => ‘welcome’
else
@login = @params[‘user’][‘login’]
flash.now[:notice] = l(:user_login_failed)
end
end


On Apr 28, 9:04 pm, Manish N. [email protected]
wrote:

if @session['user'] = User.authenticate(@params['user']['username'],

@params[‘user’][‘password’])

Don’t use @session, @params etc. They were removed. Use session/params
instead.

Fred

That looks like a good form :slight_smile:

On Mon, Apr 28, 2008 at 2:04 PM, Manish N.

Frederick C. wrote:

On Apr 28, 9:04�pm, Manish N. [email protected]
wrote:

� � if @session[‘user’] = User.authenticate(@params[‘user’][‘username’],
@params[‘user’][‘password’])

Don’t use @session, @params etc. They were removed. Use session/params
instead.

Fred

nothing happen…
i just remove wht u say …but still same error…

NoMethodError in UserController#login_submit

You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

On 29 Apr 2008, at 19:10, Manish N. wrote:

params
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

Learn to understand these errors: what line did it occur on. What is
the object that is null and why is it null etc… Step through the
code with the debugger.

Fred

Frederick C. wrote:

On 29 Apr 2008, at 19:10, Manish N. wrote:

params
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

Learn to understand these errors: what line did it occur on. What is
the object that is null and why is it null etc… Step through the
code with the debugger.

Fred

hi…
this code is running in my office pc…but whn i run same code in home
pc…
it shows error…can u tell me wht the main reason…

On Apr 30, 2008, at 2:14 PM, Manish N. wrote:

Fred

hi…
this code is running in my office pc…but whn i run same code in
home
pc…
it shows error…can u tell me wht the main reason…

Send me your pc and I’d take a look :wink:

Seriously, there’s no way that anyone here can help you with so little
information. You’ll have to work it out on your own or trade excess
punctuation for more detail.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

What Prasad said! (Please don’t double-post your issues)

-Rob

On May 1, 2008, at 2:37 AM, Prasad wrote:

# # # Authenticate method # #

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

end

HERE IS ONE LOOK OF MY FORM WITH CONTROLLER & MODEL & FORM & DATABASE

HERE IS MY ERROR -----

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’


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


HERE IS MY FORM ----

Login Section

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

Username:  <input

id=“user_username” name=“user[username]” size=“15” type=“text” />

Password:  <input

id=“user_password” name=“user[password]” size=“15” type=“password”
/>


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


CREATE TABLE users (
id int(11) NOT NULL auto_increment,
username varchar(64) NOT NULL,
password varchar(64) NOT NULL,
name varchar(200) NOT NULL,
email varchar(150) NOT NULL,
created_at datetime default NULL,
PRIMARY KEY (id)
) ;

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