Sunday, November 30, 2008

How can I create sample application using Ruby on Rails?

To create a sample web application using Ruby on Rails needs to setup environment first which I explain in my previous blog. Now we will create a sample application to manage (CRUD) user information by using NetBeans IDE.

Below are the steps to create a sample application:
  • In the NetBeans IDE, choose File > New Project.
  • Select Ruby in the Categories field and Ruby on Rails Application in the Projects field. Click Next.
  • Type sampleapplication in the Project Name field.
  • Select Ruby as a platform in place of JRuby in Ruby Platform field. Accept all the other default settings.
  • Click Finish to create the new project. You will see a list of files in the project directory and database.yml file in the editing area.
  • Create a sample database and edit the database.yml by providing the database information in the development configuration.
  • Save the database.yml file.

Creating Model, View, Controller and migration script for user management:
  • Go to the project folder from command prompt and run following command:
    ruby script/generate scaffold user name:string email:string address:text phone:string
    It will create model, views, controller and migration script in respective directory in your project and below is the output for the command:
    exists  app/models/
    exists  app/controllers/
    exists  app/helpers/
    create  app/views/users
    exists  app/views/layouts/
    exists  test/functional/
    exists  test/unit/
    create  test/unit/helpers/
    exists  public/stylesheets/
    create  app/views/users/index.html.erb
    create  app/views/users/show.html.erb
    create  app/views/users/new.html.erb
    create  app/views/users/edit.html.erb
    create  app/views/layouts/users.html.erb
    create  public/stylesheets/scaffold.css
    create  app/controllers/users_controller.rb
    create  test/functional/users_controller_test.rb
    create  app/helpers/users_helper.rb
    create  test/unit/helpers/users_helper_test.rb
    route  map.resources :users
    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    db/migrate
    create    db/migrate/20090504184109_create_users.rb
  • In the Projects window, right-click the sampleapplication node and choose
    Migrate Database > To Current Version.
    Notes: If it generates error then run gem install mysql command from command prompt and copy libmySQL.dll from C:\Program Files\MySQL\MySQL Server 5.0\bin to C:\ruby\bin.

Run and test the Application:
  • Open routes.rb under configuration node of project folder.
  • Edit following line by removing # and change welcome to users.
    # map.connect '', :controller => "welcome" or
    # map.root :controller => "welcome"
  • Delete index.html under public node which is used to show default welcome page. Rails first look to this file then look to routes.rb to figure out what page to display.
  • In the Projects window, right-click the sampleapplication node and click to Run or you can run project from command prompt by following command under project:
    ruby script\server

Project creation has been completed and you can browse it in http://127.0.0.1:3000
Now you can create, edit, show and delete user by clicking respective link.

Validating inputs:
If you want to ensure users must provide values for both name and email fields, add following lines to User class under app/model folder of your project:
class User < ActiveRecord::Base
  validates_presence_of     :name, :email
  validates_format_of       :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :message => "Invalid"
end

Now system will give error message if you try to create user without name and email.

OK, Finally we have completed the sample application with data validation. To continue development using Ruby on Rails and skills up you will get lots of resources in web.

Click here for some good resources for Web development in Ruby on Rails

2 comments:

  1. Thanks for your nice post with some helpful resource link.

    ReplyDelete