Topic: How to define our own methods in a class

I have a class which helps me to register. Now I have added a new screen so that, once the users have registered they can login by giving their user name and password. so I added the code in the controller to check for the valid username/password.

This method is for bringing up the login screen.

def login
    @register = Register.login
  end

This method checks for the valid user name /password.

def signup
   if request.post?      @register = Register.authenticate(params[:username], params[:password])
     if @register.nil?        flash[:notice] = "Invalid login" 
      redirect_to :action => 'login'
    else       
    redirect_to :action => 'form'
    end
    end
end

I have also added the code under module.

class Register < ActiveRecord::Base
   def self.authenticate(username, password)
  @register = Register.find(:first, :conditions => ['username = ?', username])
  if Register.blank? || Digest::SHA256.hexdigest(password + Register.password_salt) != Register.password_hash 
    nil
    else     
    Register
    end
end     
end


When I click on the login link I get this error "undefined method `login' for Register:Class"

Where am i wrong. Please direct me the path.

Thanks
Janakiraman

Re: How to define our own methods in a class

I have made the necessart changes to the code and the code is as follows:

controller:

def login
    @register = Register.new
  end

def signup
   if request.post?      @register = Register.authenticate(params[:username], params[:password])
     if @register.nil?        flash[:notice] = "Invalid login" 
      redirect_to :action => 'login'
    else       
    redirect_to :action => 'form'
    end
    end
end

Module:
class Register < ActiveRecord::Base
def self.authenticate(username, password)
  @register = Register.find(:first, :conditions => ['user_name = ?', username])
  if @register.blank? || Digest::SHA256.hexdigest(password + user.password_salt) != user.password_hash 
    nil
    else     
     @register
    end
end 
 
end


Now I get an error  "wrong number of arguments (1 for 0)".

Can you help me from here on. I think the number of arguments passed is wrong. Also iam not encrypting the password. It is inserted as such.


Thanks
Janakiraman

Re: How to define our own methods in a class

Your stack trace from development.log should tell you exactly what line is causing the wrong number of arguments exception...

BrewControl.com - Brewery and Brewpub management powered by RoR

Re: How to define our own methods in a class

Thanks for the info.

This is what I get when I opened the development log file.

ArgumentError (wrong number of arguments (1 for 0)):
    /app/controllers/register_controller.rb:28:in `post?'
    /app/controllers/register_controller.rb:28:in `signup'
    c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1095:in `send'
    c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1095:in `perform_action_without_filters'
    c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:632:in `call_filter'
    c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:634:in `call_filter'


Iam not sure, where am i wrong.

Thanks
Janakiraman

Re: How to define our own methods in a class

Is any one there to point out where am i wrong. Iam waiting for the answers. please help me.

Thanks
Janakiraman

Re: How to define our own methods in a class

You need to clean up the code. Ruby expects separate statements to be on different lines. Looks like this is your problem.

def login
  @register = Register.new
end

def signup
  if request.post?
    @register = Register.authenticate(params[:username], params[:password])
    if @register.nil?
      flash[:notice] = "Invalid login" 
      redirect_to :action => 'login'
    else       
      redirect_to :action => 'form'
    end
  end
end

# model
class Register < ActiveRecord::Base
  def self.authenticate(username, password)
    @register = Register.find(:first, :conditions => ['user_name = ?', username])
    if @register.blank? || Digest::SHA256.hexdigest(password + user.password_salt) != user.password_hash 
      nil
    else     
      @register
    end
  end
end

Railscasts - Free Ruby on Rails Screencasts

Re: How to define our own methods in a class

Thanks a lot!!!. I was struggling hard to get this done. Now I dont get an error. But when I enter the username/password, I dont get an error message nor the page is getting redirected. The same page is getting refreshed.

can you point to me as where am i wrong

Thanks a lot for all your help.

Thanks
Janakiraman

Re: How to define our own methods in a class

my guess it is getting redirected to the same page, it is just happening so fast it looks like it's refreshing it. Normally it should display the "Invalid login" flash message, but perhaps you aren't outputting it. Do you have this somewhere in the template or layout?

<%= flash[:notice] %>

If not, add it and try again, see if you get the "Invalid Login" message.

Railscasts - Free Ruby on Rails Screencasts

Re: How to define our own methods in a class

Great!!! Thanks a lot!!! It did come. But when I give the correct user name and password i get 'Invalid Login'. The reason I think is the password in the database is not encrypted, but while checking for the password i think so it is encrypted. so what should I should and in what way it is encrypted so that i can follow the same while inserting records.

Thanks a lot!!!

Thanks
Janakiraman

Re: How to define our own methods in a class

This is the line that encrypts the password:

Digest::SHA256.hexdigest(password + user.password_salt)

It merges the password with the password_salt before encrypting so that's what you would need to do. I recommend doing this in a before_create callback or something.

Railscasts - Free Ruby on Rails Screencasts

Re: How to define our own methods in a class

My sincere thanks to you. you are rolling. Now an user could login and view details. I need to send a mail to the registered person once he registers with our site. I have a default that we sent to the users once they have successfully registered. But How to achieve this using ruby on rails. Iam helpless in this regard.


Thanks
Janakiraman

Re: How to define our own methods in a class

To send mail using rails, try ActionMailer.

Railscasts - Free Ruby on Rails Screencasts

Re: How to define our own methods in a class

Thanks. It was very useful. I did follow the instruction given there. I get an error saying 'undefined method 'email_address_with_name'. so what is that I should use there.
recipients recipient.email_address_with_name.

secondly I have used like this. Is it correct?

Notifier.deliver_signup_notification(params[:register]). Iam not sure whether I have used it in the correct way. please guide me. I should pass the username to the module method. how to do that?


Thanks
Janakiraman