Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 4 - Ruby-on-Rails: Understanding the Basics of...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Ruby-on-Rails: Understanding the Basics of Active Record
By: APRajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2006-10-11

    Table of Contents:
  • Ruby-on-Rails: Understanding the Basics of Active Record
  • Active Record: What is it?
  • Active Record: Steps to using it
  • Rails in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Ruby-on-Rails: Understanding the Basics of Active Record - Rails in the Real World


    (Page 4 of 4 )

    Without much ado let's get to work and see how to implement a login module. The requirements of the module are as below:

    • A form that allows the users to enter their user name and password.
    •  A simple userid password verification implemented to check the credentials of the user.
    • Once they are logged in, we need to record the fact somehow for the rest of their session (or until they log out).

    On the basis of the requirements, the following are the components required:

    • User.rb – Model for the user table.
    • login_controller.rb – The controller orchestrating the flow.
    • login.rhtml – View providing form for logging in.

    Here is the user table:

    
    

    create table users ( id int not null auto_increment, name varchar(100) not null, password char(40) null, primary key (id);

    Next comes the model class. Since we are using the Active Record in RoR, there is no need to use the establish_connection method. Instead I will be using the database.yml file with the following information (since I am expanding upon the demo application created in the first part):

    
    

    development: adapter: mysql database: demo_development username:raj      password:       host: localhost

    The command to generate the model class is:

    ruby script/generate model User

    The model class contains two methods- try_to_login which in turn calls login. Here is the code:

    
    

    class User < ActiveRecord::Base def self.login(name, password) find(:first, :conditions =>
    ["name = ? and hashed_password =    ?", name,password]) end def try_to_login User.login(self.name, self.password) end end

    The login method contains the find method with criteria. The details of this will be covered in the next part. The logic is simple – the try_to_login method gets called by the controller. This method in turn passes the data to the login method. The requirement of layering is to provide implementation abstraction. The login method tries to retrieve the data corresponding to username and password (before anyone complains about lack of security please bear with me as this is not a full-fledged implementation).

    Next comes the controller. For that here is command:

    ruby script/generate controller Login

    And here is the code that implements the login validation and session establishment.

    
    

    class LoginController < ApplicationController def login @user = User.new(params[:user]) logged_in_user = @user.try_to_login if logged_in_user session[:user_id] = logged_in_user.id
    #creating the session #and putting user’s id in   #it redirect_to(:action => "index") else flash[:notice] =
    "Invalid user/password combination" end end end

    And here is the view :

    <% @page_title = "Add a User" -%>

    <%= error_messages_for 'user' %>

    <%= form_tag %>

    <table>

    <tr>

    <td>User name:</td>

    <td><%= text_field("user", "name") %></td>

    </tr>

    <tr>

    <td>Password:</td>

    <td><%= password_field("user", "password") %></td>

    </tr>

    <tr>

    <td></td>

    <td><input type="submit" value=" ADD USER " /></td>

    </tr>

    </table>

    <%= end_form_tag %>

    That completes the login application. If you compare it with similar applications in any other language, the result would tell you that the number of lines is really less in RoR-based code. In this discussion there are certain aspects of Active Record that I have left out. The left out pieces of the puzzle called Active Record will be covered in the next part. Till then…


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway