Intro to Ruby on Rails
Ruby on Rails, often shortened to Rails or RoR, is an open source web application framework for the Ruby programming language that makes it easier to develop, deploy and maintain web apps. Rails uses the Model-View-Controller (MVC) architecture pattern to organize application programming.
It includes tools that make common development tasks easier “out of the box”, such as scaffolding that can automatically construct some of the models and views needed for a basic website. Also included are WEBrick, a simple ruby web server, and Rake, which is a build system.
Together with Rails these tools provide a basic development environment. One of Rails’ biggest strengths is metaprogramming, Rails introduces the Active Record framework, a design pattern by Martin Fowler. The rails version of Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming. Rails also relies on convention over configuration. If you follow the suggested naming conventions, Rails does not need much configuration. Rails also aides in creating start-up code using scaffolding so that you can get started on your code as soon as possible. These are few of the many features of Rails.
So let’s create a simple rails application! If you are using a Mac, Ruby on Rails is shipped with Mac OS X v10.5 and above. If you don’t have it installed it already, you can go to RubyonRails.org to download it for your target operating system. I am currently using Rails version 2.3.5. Also, I am going to be using NetBeans as my editor (it’s free!). Another very popular editor of choice if you are using a Mac is TextMate. I will be using the MySQL community server as my database and the MySQL Workbench tool, but you can easily use the database of your choice. SqLite 3 is the default database for Rails, starting with version 2.0.2. With SQLite 3 there are no steps required to create a database, and comes in handy if you need to get started quickly.
Open up the terminal window and navigate to a directory in your file system where you would like to create your application. For the purpose of this post, let’s say my working directory is
/code/rails. Once you are in your working directory, use the rails command to create an application called demo.
rails --database=mysql demo
Once you have run the rails command, a directory named demo will be created with a bunch of files and subdirectories inside it. Open up the project using your favorite editor. Notice the directory structure rails has created for you. When I used the rails command, I used the
--database switch, which tells rails which database to use, which is MySQL in our case. Now open up the project (under
/code/rails/demo) and the directory structure should like this:
Open up the database.yml file under the
/demo/config directory. Database.yml is the configuration file rails uses to connect to the database. Your configuration should look like:
database.yml contains 3 sections, one each for the development, test, and production databases. Each parameter has the following meaning:
adapter: the type of database
database: name of the database
username and password: credentials for the application to connect to the database
host: name of the machine hosting the database server.
Next, navigate to the
demo directory and run the following command:
This will create the
demo_development database. Next, run the migrate command:
This will create a
schema_migrations table inside the database. Look at the figure below:
Schema_migrations is the table that supports Migrations in rails. A migration represents a change we want to make to the database, expressed in a source file in database-independent terms. These migrations can be applied to make updates to the database, and also rollback those changes. Next, we are going to use the generate command to create a new model. Rails automatically creates a migration that can be used to create the corresponding table. For the sake of this demo, let’s say we are trying to create a shopping cart. Users can view products in our inventory, and add the products they want to buy in the cart.
ruby script/generate scaffold product name:string description:text price:decimal
Next run the
The above two commands create the product model, and also the products table inside our database. Note that we use “product” on the command line. In rails, a model (product) is automatically mapped to database table (products… plural form of the model’s class). The rails generator created the model (
/demo/app/controllers/products_controller.rb), views (edit.html.erb, index.html.erb,new.html.erb, show.html.erb inside
/app/views directory), and the controller (products_controller.rb under
Also, inside the db directory, rails creates the migration file (
db/migrate/20100801185457_create_products.rb). The migration has a UTC-based timestamp prefix (20100801185457 in this demo, yours will be different), a name (create_products) and a file extension (.rb).
Next, you need to run this file to create the database. If you open this file, it looks something like this:
class CreateProducts < ActiveRecord::Migration def self.up create_table :products do |t| t.string :name t.text :description t.decimal :price t.timestamps end end def self.down drop_table :products end endRails keeps track of the migrations which have been applied to the database. After you run the rake db:migrate command, go to the database, and you will see the products table created with the correct schema.
How does rails know which migrations have and have not been applied to the database? Take a look at the schema_migrations table after running the migration, and you will see an entry in the database with the value being 20100801185457, corresponding to the migration just ran.
At this point, you have an application with a database you can connect to. I am going to be using the WEBrick based web server supplied with rails. Run the following command:
This starts a web server on the local host, port 3000. Next open up a browser window and type the url http://localhost:3000/products This .brings up the index view. How does rails know which controller action to call. To understand that, we need to look at rails routing. Open up the routes.rb file under the config directory. You will find the following line in there:ruby script/server
map.resources :productsThat line is a way of defining RESTful resources. Map.resources will map the following actions in the corresponding controller: index, new, create, show, edit, update. Navigate the different pages of the application, and you should be able to create, view, update and delete a product. That’s all for creating a simple application to get started. In following posts, we will be looking at some more features rails has to offer.
Jas Singh is a Technical Lead for Fellowship Technologies. He has been with Fellowship Technologies since July 2007. He believes that every new day brings opportunities to learn something new.
- Include Requirements & Contribution Sub Types
- User Case Story from Hope Community Church
- Group Search Categories and More
- Account Creation
- Single Sign On Functionality Exposed
- API Communication Value Changes
- API Enhancement: Create and Edit Groups!
- API Enhancement: Requirements Exposed
- Resource Versioning
- Enter Visitor Data via Your Church Website
- Fellowship One & Planning Center Online
- API Libraries and Sample Code
- Building a custom login for your church website using the API
- Roll Foward!
- The Agile Triangle
- Conversation Paralysis
- Picture this, image updates & creates through the REST API
- A REST API double shot : Groups and Events realms
- Increasing Software Delivery by 500%
- Quick people API realm update
- Introducing the new REST API giving realm
- Raising the bar…
- Building a Deployment Pipeline
- The World of Dev Craft
- Running Tests in Parallel with Selenium
- Abstracting Your Code to Remove Duplication
- Documentation in an Agile Environment
- Drowning in Debt
- Intro to Ruby on Rails
- API Strategy & Roadmap
- Staging/Sandbox Environment is Back up!
- Downtime in Sandbox/Staging Environment
- Android & OAuth
- F1 API Static Library with Objective-c
- Programming in F#
- NoSQL: HuMONGOus Benefits (Part 2)
- Our Scrum Team Structure
- SaaS & BI - The History & Future
- Getting Started with Android
- NoSQL: Leaving Schema Behind (Part 1)
- Your Feedback…and a $25 Gift Card!
- A Scrum Ceremony? Is this a wedding or something?
- Variables in PHP
- Data Exchange API Fixes
- F1 Check-in on the iPad
- Be the first to get the news & tips!
- An Introduction to PHP
- Working with Pop Up Windows in Selenium
- List Comprehension
- Source Control: A Time Machine For Your Source Code
- Developer Conference…Lower Price, Same Great Content!
- The Quality Assurance Team
- How does Fellowship Technologies manage complex projects?
- Developer Conference coming in May!
- Sandbox Refresh Complete
- Sandbox Refresh This Week
- Updates coming to the REST API
- Sandbox Environment Down Time
- F1Touch :: Fellowship One On The Go
- Under the Hood
- Sandbox Refresh Complete
- Sandbox Refresh Tomorrow (Oct. 2nd)
- Fellowship One Developer Forums
- Ten Commandments of API Consumption
- REST API Enhancements / Fixes deployed to Sandbox and Production 09.09.09
- Data Exchange URL cut-over complete
- Important Data Exchange URL changes
- Ron Nom Nom
- How to get started using the REST API