Every Flavour Beans

“The time has come…to talk of many [technologies].” –Lewis Carroll(‘The Walrus and the Carpenter’)
Development Tools. Web Frameworks. GNU/Linux. Nokia N800. Video Encoding.

January 11, 2009

“Hello, World” Web Application in Ruby on Rails using console

Filed under: Ruby/Rails — tabrez @ 6:19 pm

Installation and setup instructions of Ruby on Rails web framework on different operating systems is covered in the following posts:

  1. Setting Up Rails Development Environment on Windows Vista/XP
  2. Setting Up Rails Development Environment on Ubuntu GNU/Linux
  3. Setting Up Rails Development Environment on Fedora GNU/Linux

If you would rather use an IDE to develop Ruby on Rails applications, Aptana IDE is covered in "“Hello, World” Web Application in Ruby on Rails using Aptana Studio."

This post describes how to create a basic "Hello, World" web application in Rails using only console tools and a text editor. The instructions work pretty much the same for all operating systems with little to no modifications.

Make sure that you have the latest versions of all components installed:

# ruby -v && gem -v && rails -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-linux]
1.3.1
Rails 2.2.2

(Output text might be slightly different on Windows OS but the version numbers should be the same.)

To create a new rails project, run the following command which generates the required directory structure for a rails application:

# rails hello
# cd hello
# ls
app db lib public README test vendor
config doc log Rakefile script tmp

The purpose of each directory generated is more or less self-explanatory, like the test directory is for storing test files and the log directory contains various log files.

Rails application the scaffold generator way

Scaffolding allows us to generate template code necessary to directly run our application and play with it without having to write a single line of code. We can also open the generated files to take a peek at the generated code. We can then modify this code to our liking or just throw it away and write everything manually once we are satisfied with the prototype. The scaffolding code can be generated, say for a resource called 'person', by running the following command:

# ruby script/generate scaffold person name:string password:string email:string age:integer

This command generates(among other files) a Rails migration file in db/xxx_create_people.rb which is responsible to create the database schema for our application; a model in app/models/person.rb; a controller in app/controllers/people_controller.rb and related view files in app/views/people directory. ('people' is used wherever plural of 'person' is needed.) Have a look at the code generated in the migration file:

RUBY:
  1. create_table :people do |t|
  2.       t.string :name
  3.       t.string :password
  4.       t.string :email
  5.       t.integer :age
  6.  
  7.       t.timestamps
  8.     end

Run the following commands to initialize the database:

# rake db:create:all
# rake db:migrate

Now we are ready to run the application and use it. Start the WEBrick web server:

# ruby script/server

Open your favourite web browser and go to http://localhost:3000/persons url. You should see a page similar to the following screenshot(after I added couple of entries using the "New Person" link):

Also make sure you look at the code generated in the controller file and the various view files!
Read the official Getting Started with Rails guide.
Read more Rails articles on this blog.


If you want to receive future posts by email, enter your email address here:

Related Posts:

  • “Hello, World” Web Application in Ruby on Rails using Aptana Studio
  • Setting Up Rails Development Environment on Fedora GNU/Linux
  • Setting Up Rails Development Environment on Ubuntu GNU/Linux
  • Setting Up Rails Development Environment using Aptana Studio
  • Setting Up Rails Development Environment on Windows Vista/XP
  • Setting Up Ruby on Rails Projects with Git and Github
  • Setting Up Development Environment For Grails on Windows Vista/XP


  • September 15, 2008

    Installing amazon-ecs/hpricot RubyGem on Windows Operating System

    Filed under: Ruby/Rails — tabrez @ 6:45 pm

    Installing rubygems that come with native extensions is not same on gnu/linux and Windows platforms as I have discovered recently. Apparently, it is important that the native extensions needed by the rubygem be built with the same compiler tool chain as your main ruby installation. The rubygem I needed to install was amazon-ecs and it depends on another rubygem called hpricot which comes with native extensions. Though hpricot installed fine on gnu/linux OS, I recieved the following errors on Windows:

    C:\Users\tabrez> gem install hpricot
    Bulk updating Gem source index for: http://gems.rubyforge.org/
    Building native extensions. This could take a while...
    ERROR: Error installing hpricot:
    ERROR: Failed to build gem native extension.

    c:/ruby/bin/ruby.exe extconf.rb install hpricot
    checking for stdio.h... no
    *** extconf.rb failed ***
    Could not create Makefile due to some reason, probably lack of
    necessary libraries and/or headers. Check the mkmf.log file for more
    details. You may need configuration options.

    Gem files will remain installed in c:/ruby/lib/ruby/gems/1.8/gems/hpricot-0.6.161 for inspection.
    Results logged to c:/ruby/lib/ruby/gems/1.8/gems/hpricot-0.6.161/ext/fast_xs/gem_make.out
    C:\Users\tabrez>

    Help on the IRC channel and some searching on the Internet provided me with the following solution: use whytheluckystiff's repository as the source to install hpricot rubygem. Once hpricot rubygem was installed, amazon-ecs rubygem installed with no problems.

    C:\Users\tabrez> gem install hpricot ––source http://code.whytheluckystiff.net
    Successfully installed hpricot-0.6-x86-mswin32
    1 gem installed
    Installing ri documentation for hpricot-0.6-x86-mswin32...
    Installing RDoc documentation for hpricot-0.6-x86-mswin32...

    C:\Users\tabrez> gem install amazon-ecs
    Successfully installed amazon-ecs-0.5.3
    1 gem installed
    Installing ri documentation for amazon-ecs-0.5.3...
    Installing RDoc documentation for amazon-ecs-0.5.3...
    C:\Users\tabrez>

    The same source works for sandbox rubygem too.

    References:


    If you want to receive future posts by email, enter your email address here:

    Related Posts:

  • Why I Use iPod as a USB Storage Device
  • Boost Filesystem Library: Writing Portable C++ Programs to Acess The Filesystem
  • Linux Bible 2007 Edition: Install/Run 10+ GNU/Linux Distributions(Ubuntu, Fedora, Gentoo etc)
  • C++ Boost Filesystem Library(Part II): Example Programs
  • Intel’s Threading Building Blocks(TBB) Library Available under GPL
  • Setting the Stage for C++ Boost
  • Creating “Hello World” Web Application Using the Grails Framework


  • September 6, 2008

    “Hello, World” Web Application in Ruby on Rails using Aptana Studio

    Filed under: Eclipse, Ruby/Rails — tabrez @ 2:34 pm

    I am assuming that you have everything properly setup on your computer for developing applications with the Ruby on Rails framework using Aptana Studio IDE. If not, see my previous posts:

    In this post I will take you through a screenshot guided tour of the Aptana Studio (RadRails) IDE and show you how to develop a blank web application using Ruby on Rails framework and add a simple scaffold to it("Hello, World" equivalent for Rails framework). So let's get started.

    When you run the Aptana Studio application, you will see the splash screen as shown below:
    Aptana Studio IDE splash screen.

    To create a new Ruby on Rails project, go to File -> New -> Project... and select Rails Project from the "Rails" folder(expand it if necessary). Enter a name for the project(say "Demo1") and click Finish.
    Create a new Ruby on Rails web application project in Aptana Studio IDE

    The main window should look like the following screenshot(click to see larger version).
    Create a new Ruby on Rails web application project in Aptana Studio IDE

    Below you can see the screenshots of various sections of the main window shown above:
    Create a new Ruby on Rails web application project in Aptana Studio IDE

    The Project pane on the left:
    Create a new Ruby on Rails web application project in Aptana Studio IDE

    The Console window at the bottom:
    Create a new Ruby on Rails web application project in Aptana Studio IDE

    The Scripts window at the right(you can access the same from the "Scripts" menu too):
    Create a new Ruby on Rails web application project in Aptana Studio IDE

    The Cheat Sheets window at the extreme right:
    Ruby on Rails cheatsheet in Aptana Studio IDE

    If you click the "Click to Begin" link in the cheatsheet window(shown above), you will see the following "Dynamuc Help" window with links to various Rails help topics:
    Ruby on Rails help topics in Aptana Studio IDE

    Aptana Studio supports the following views that you can enable, for example to view API documentation, try out some regular expressions etc.:
    Different views supported for Ruby on Rails web application project in Aptana Studio IDE

    At the bottom, you can click on RubyGems tab to see a list of Ruby gems:
    List of RubyGems available in Aptana Studio IDE

    You might be prompted to install some Ruby gems by Aptana Studio(you can install or ignore them):
    Install RubyGems right from the Aptana Studio IDE

    RailsPlugins tab will show you the Rails plugins that you can install with a few simple mouse clicks:
    Aptana Studio shows the Rails plugins that you can install with a few  mouse clicks

    RakeTasks tab allows you to perform various rake tasks by selecting the one you want from the drop-down box:
    Perform various rake tasks just by selecting the one you want from the drop-down box in Aptana Studio IDE

    Generators tab similarly allows you to generate a scaffold, model, controller, view etc. by selecting the appropriate option from the drop-down box:
    Generate a scaffold, model, controller, view etc. by selecting the appropriate option from the drop-down box in Aptana Studio IDE

    Creating a Blank Rails Web Application using Aptana Studio

    Let us put the Generators tab to use. Select "Scaffold" from the "Generator" dropdown box, enter the parameters as shown in the screenshot below and press the "Go" button.
    Generate a Rails scaffold and model in Aptana Studio IDE

    You should see the following output in the Console tab.
    Scaffold generation output in Aptana Studio console window

    You should be able to see the following migration file generated in the project directory:
    Migration file generation output in Aptana Studio in console window

    And here is a screenshot showing the contents of the generated migration file:
    Generated Rails migration file by the scaffold generator in Aptana Studio IDE

    If you now run the rake tasks "db:create:all" and "db:migrate" you should be able to see the output of commands executed in the console window:
    Output of Rake database migration tasks in Aptana Studio console window

    Now run the application. You should see the following output in the internal web browser of Aptana Studio:
    Ruby on Rails web application running in the internal web browser of Aptana Studio IDE

    Click on the "New Book" link and you will see the new book form like this:
    Ruby on Rails web application running in the internal web browser of Aptana Studio IDE

    You can go back to your Rails project, make edits, generate more scaffolds, models, controllers and views and test your changes in the internal browser of Aptana Studio. And so on and so forth.

    Aptana Studio IDE with RadRails plugins provides one of the richest web application development environments for Ruby on Rails framework. NetBeans 6.1, emacs/vim, IntelliJ IDEA are pretty good too and all of these are multi-platform, so why bother with Mac-only solution like Textmate? ;)


    If you want to receive future posts by email, enter your email address here:

    Related Posts:

  • Setting Up Rails Development Environment using Aptana Studio
  • “Hello, World” Web Application in Ruby on Rails using console
  • Setting Up Rails Development Environment on Windows Vista/XP
  • Setting Up Rails Development Environment on Fedora GNU/Linux
  • Setting Up Rails Development Environment on Ubuntu GNU/Linux
  • Setting Up Ruby on Rails Projects with Git and Github
  • Setting Up Development Environment For Grails on Windows Vista/XP


  • September 3, 2008

    Setting Up Rails Development Environment using Aptana Studio

    Filed under: Ruby/Rails — tabrez @ 5:37 pm

    You first need to complete the first five steps of my earlier post "Setting Up Rails Development Environment on Windows Vista/XP" which explain how to install Ruby, RubyGems, Ruby on Rails, Mongrel web server and MySQL database server on Microsoft Winodws Vista/XP. You can skip the last two steps(sixth and seventh) of that post which describe how to setup NetBeans 6.1 IDE for development with the Rails framework; continue with this post instead which explains how to setup the Eclipse-based IDE Aptana Studio for developing web applications using the Ruby on Rails framework.

    Download and Install Aptana Studio and RadRails plugin

    1. Download Aptana Studio IDE, run the installer and install it on your computer(or simply extract it if you downloaded the zip file).

      Download Eclipse-based IDE called Aptana Studio with RadRails for Ruby on Rails

    2. Run Aptana Studio. In the main window, you should see the start page of Aptana Studio(for some reason if you don't, go to Help -> Aptana Studio Start Page...). Here you will see options to install various plugins for Aptana Studio like PHP plugin, Ruby on Rails plugin, Adobe AIR plugin etc. Click the "Install" button in the "Ruby on Rails" section.
    3. Select Aptana RadRails checkbox and select all the available optional features available for it.
      Install RadRails plugin for Eclipse-based Aptana Studio IDE.

      Install RadRails plugin for Eclipse-based Aptana Studio IDE.

      I recommend that you leave the selected default location for installation as it is and proceed with the installation.
      Install RadRails plugin for Eclipse-based Aptana Studio IDE.

    Once the RadRails plugin is installed, it is time to configure Ruby installation in Aptana Studio. If you haven't installed Ruby and Ruby on Rails yet, see my previous post.

    Configure Ruby/Rails in Aptana Studio

    1. If Ruby is installed in a location like "C:\ruby", Aptana will automatically find the path and configure the Rails environment so you don't have to worry about anything. If you have installed Ruby in a non-standard path where Aptana could not find it, then you need to set the path manually. To do so, go to Windows -> Preferences, expand the Ruby node in the left pane and select the Installed Interpreters item.

      Set the path to installed Ruby interpreter in Aptana Studio RadRails IDE

    2. Click the Add button and enter the details about your Ruby installation directory. Paths to all the other Ruby tools(e.g. rake, rails etc.) will be picked up automatically from this setting.

      Set the path to installed Ruby interpreter in Aptana Studio RadRails IDE

    That's all we need to do to get Ruby on Rails framework configured with Aptana Studio IDE. We can now create new project and check out if everything is working properly. I have made a separate post to show how to create a basic Ruby on Rails web project in Aptana Studio and run it with the help of a lot of screenshots to introduce you to the most important features of the Aptana RadRails IDE for Rails based development, so check that out for more details.


    If you want to receive future posts by email, enter your email address here:

    Related Posts:

  • “Hello, World” Web Application in Ruby on Rails using Aptana Studio
  • “Hello, World” Web Application in Ruby on Rails using console
  • Setting Up Rails Development Environment on Fedora GNU/Linux
  • Setting Up Rails Development Environment on Windows Vista/XP
  • Setting Up Rails Development Environment on Ubuntu GNU/Linux
  • Setting Up Ruby on Rails Projects with Git and Github
  • Six Popular IDEs For Developing Software in C/C++ on Windows Platform


  • July 22, 2008

    Setting Up Rails Development Environment on Ubuntu GNU/Linux

    Filed under: Ruby/Rails, Ubuntu — tabrez @ 7:38 pm

    This is an adaptation to Ubuntu GNU/Linux platform of my previous post which was meant for the Windows platform: Setting Up Rails Development Environment on Windows Vista/XP

    I can think of two different ways in which you might want to set up a development environment for Ruby on Rails on a GNU/Linux machine. One is to download everything outside of the package manager of your distribution i.e. build everything from the source. I am going to cover the second, easier way: how to setup Rails on a Debian/Ubuntu machine using its package manager for the most part. I show below how to setup Ruby, RubyGems, Rails, Mongrel, MySQL, and Subversion as part of the development stack.

    1. Installing Ruby:

      If not already installed, install the build-essential package first:

      # sudo aptitude install build-essential

      Then install the Ruby and related packages:

      # sudo aptitude install ruby irb ri rdoc rake libopenssl-ruby libsqlite3-ruby ruby1.8-dev
    2. Installing Rubygems

      Though it is possible to install Rubygems using the Ubuntu package manager, not only will you get an old version of rubygems if you do so, it also may not be compatible with the latest version of rails. I strongly recommend you download the source code package of the latest version of rubygems and install it using its setup script:

      # cd $HOME
      # wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
      # tar xvzf rubygems-1.2.0.tgz && cd rubygems-1.2.0
      # sudo ruby setup.rb

      Debian purists may frown at installing packages all over the system directories outside of control of the package manager. See Debian’s position on Rubygems. [via - RailsOnUbuntu]
      Remember to download the latest version of rubygems available(currently 1.2.0). You can remove the downloaded tgz archive and the extracted directory after the installation is finished.

      #cd $HOME
      # rm -r rubygems-1.2.0.tgz rubygems-1.2.0

      If Ubuntu complains that the 'gem' command is not found, you may have to create a symbolic link like this:

      # sudo ln /usr/bin/gem1.8 /usr/bin/gem

      You can update the rubygems package by running the following command:

      # sudo gem update ––system
    3. Installing Rails

      The final step is to install the Rails gem package itself.

      # sudo gem install rails

      This will install the latest Rails version on your Ubuntu GNU/Linux. If you ever want to update Rails in the future, just run the following command:

      # sudo gem update rails

      You can update the other packages using Ubuntu's package management tools(aptitude) since that is how you installed those packages in the first place.

    The basic Rails development environment is now installed on your system and you can skip the rest of the post if you are happy with WEBrick as the web server, SQLite as the database server and your favourite text editor/IDE that may be already installed on your system. The following instructions cover installing an alternate web server called Mongrel; MySQL database server and its GUI tools; and Subversion.

    1. Installing Mongrel

      Installing the Rails gem also installs the WEBrick web server which is ideally suited for development purposes. Another much recommended web server for Rails development as well as production environment is the Mongrel web server. To install the Mongrel web server, run the following gem command:

      # sudo aptitude install mongrel

      After installing Mongrel, Rails automatically starts the Mongrel instead of WEBrick web server when you run the Rails applications in development mode. (Refer to Mongrel documentation to know more about runing Rails applications under Mongrel in production mode.)

    2. Installing MySQL

      This step covers the installation of MySQL database server and its GUI tools. Skip this step if you want to use some other database server. Run the following command to install the required mysql packages:

      # sudo aptitude install mysql-server mysql-admin mysql-query-browser

      One important step during the configuration process is to select a root password(you will need to enter it in the database.yml configuration file of your Rails application).

    3. Installing Subversion

      Finally, install your favourite revision control software - CVS, Subversion, git etc. You can install subversion in this way:

      # sudo aptitude install subversion

    Now you can create/checkout Rails applications, edit the files using your favourite text editor/IDE and run it under Mongrel. In future, I will try to write about developing Rails applications using integrated development environments like Eclipse and NetBeans.


    If you want to receive future posts by email, enter your email address here:

    Related Posts:

  • Setting Up Rails Development Environment on Fedora GNU/Linux
  • “Hello, World” Web Application in Ruby on Rails using console
  • Installing Sun Java SE 6, Apache Maven 2 and Tomcat 5.5 on Ubuntu GNU/Linux
  • Setting Up Rails Development Environment using Aptana Studio
  • “Hello, World” Web Application in Ruby on Rails using Aptana Studio
  • Installing Grails in Ubuntu GNU/Linux Using Package Manager
  • Setting Up Ruby on Rails Projects with Git and Github


  • Next Page »

    Copyright (c) 2006, 2007 Tabrez Iqbal.
    Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. Verbatim copying and distribution of this entire article is permitted in any medium without royalty provided this notice is preserved. A copy of the license is included in the section entitled "GNU Free Documentation License".


    Powered by WordPress
    This website is hosted by Dreamhost