Wednesday, December 28, 2005

1. Starting Application Development

Cool. We have setup a web server and a database within an hour. 10x productivity, you bet!

Ok, so now we can get started on customizing our app. I am not going to go deep into specific rails concepts such as naming the db tables in plural and model and controller singular and so on since all other resources pound on them.

User
  1. We have already created a "users" table in MYSQL. Here is the schema:
    id - primary key, unsigned, auto_increment, not null
    username - varchar(20)
    password - varchar (10)
    email - varchar (50)
    created_at - datetime, not null
    updated_at - datetime, not null

    Rails will automatically maintain id, created_at and updated_at columns for us. Note that these column names are special and Rails knows what to do with them if it encounters these in your table.
  2. Lets create the scaffolding for our User table.
    cd demo; ruby script/generate scaffold User user

    Note the arguments to scaffold. You need to specify both the model (the first argument) and the controller (the second argument). If you skip the controller name. the code generator generates users_controller.rb and similarly named views.

    Also you can use -p (--pretend) option to scaffold code generator which will show what changes will be made, but not make them.


    If you create the scaffolding before creating the database tables, the code generator errors out. So create the db tables before running the scaffolding generator. This will also test the connection to the db so check in config/database.yml to make sure you have specified the

    The output should be a a whole bunch of files in various sub-directories of demo.
    E:\demo>ruby script/generate scaffold User user
    exists app/controllers/
    exists app/helpers/
    create app/views/user
    exists test/functional/
    dependency model
    exists app/models/
    exists test/unit/
    exists test/fixtures/
    create app/models/user.rb
    create test/unit/user_test.rb
    create test/fixtures/users.yml
    create app/views/user/_form.rhtml
    create app/views/user/list.rhtml
    create app/views/user/show.rhtml
    create app/views/user/new.rhtml
    create app/views/user/edit.rhtml
    create app/controllers/user_controller.rb
    create test/functional/user_controller_test.rb
    create app/helpers/user_helper.rb
    create app/views/layouts/user.rhtml
    identical public/stylesheets/scaffold.css
  3. Start the webserver and navigate to the user pages http://127.0.0.1:3000/user
    cd demo; ruby script/server

    You should see the list action, which is called by the default index action :
    app/controller/user_controller.rb:
    def index
    list
    render :action => 'list'
    end

    In another window, do a tail -f log/development.log to see the webserver messages.

  4. Click on the New User link at the bottom of the page and enter the info and press Create.
    Congrats. This is so cool. You are already productive.

Installation and configuration

Excellent Installation instructions at OnLamp.com.

  1. Ruby download and setup - follow instructions from the above link
  2. Rails download and setup - follow instructions from the above link.
    You can use gem list --local to list the versions of tools installed and gem help commands for more commands such as gem update.
    I couldn't find a -n option to gem, something which will just show me the commands it will be executing.
  3. MYSQL setup - follow the ONLamp.com instructions.
  4. I used SQLyog, a free MYSQL GUI to perform the database setup.
    • created databases demo_development, demo_test and demo_production
    • created a user 'chetan'@'localhost' using Tools -> User Manager -> Add User
    • granted All permissions for this user using Tools -> User Manager -> Manage Permissions
    • created a table users with relevant fields
  5. Generate the rails app using the command
    rails demo
  6. Make sure the app was properly generated by starting the included WEBrick web server.
    cd demo; ruby script/server
    Navidate to 127.0.0.1:3000
  7. Update the demo/config/database.yml file with the database connection information. You can comment out socket: /path/to/your/mysql.sock line from the database.yml file.
    So for development the section in my demo database.yml looks like
    development:
    adapter: mysql
    database:demo_development
    username: chetan
    password: xxxxxxxxxxxxx
    # socket: /path/to/your/mysql.sock

Creating a Demo Website

Hello World!
This blog will chronicle my experiences playing with the much hyped Rails framework. I am in the process of learning Ruby and Rails and building a dummy demo. The demo I am building is a hodge podge of various website patterns, like User registration and login, creating sessions using cookies and MYSQL, Pagination etc. As I develop these, I will get more comfortable with Rails and Ruby.
I am using Agile Web Development with Rails as well as the Programming Ruby - the Pickaxe book for reference. Great deal on pdfs at http://pragmaticprogrammer.com/with free upgrades for life. So here goes...

Update:
I am now using Radrails for IDE (very cool) and Subversion (see here for how to install subversion for windows) for revision management.