Rails
Rails is a web development framework written in the Ruby language. It is designed to make programming web applications easier by making several assumptions about what every developer needs to get started. It allows you to write less code while accomplishing more than many other languages and frameworks. Long-time Rails developers also report that it makes web application development more fun.
Rails is opinionated software. That is, it assumes that there is a best way to do things, and it’s designed to encourage that best way – and in some cases to discourage alternatives. If you learn “The Rails Way” you’ll probably discover a tremendous increase in productivity. If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience.
The Rails philosophy includes several guiding principles:
* DRY – “Don’t Repeat Yourself” – suggests that writing the same code over and over again is a bad thing.
* Convention Over Configuration – means that Rails makes assumptions about what you want to do and how you’re going to do it, rather than letting you tweak every little thing through endless configuration files.
* REST is the best pattern for web applications – organizing your application around resources and standard HTTP verbs is the fastest way to go.
Rails is organized around the Model, View, Controller architecture, usually just called MVC.
MVC benefits include:
* Isolation of business logic from the user interface
* Ease of keeping code DRY
* Making it clear where different types of code belong for easier maintenance
Models
A model represents the information (data) of the application and the rules to manipulate that data. In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, one table in your database will correspond to one model in your application. The bulk of your application’s business logic will be concentrated in the models.
Views
Views represent the user interface of your application. In Rails, views are often HTML files with embedded Ruby code that performs tasks related solely to the presentation of the data. Views handle the job of providing data to the web browser or other tool that is used to make requests from your application.
Controllers
Controllers provide the “glue” between models and views. In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.
Playing With Rails
We will try to create a small web application in the Rails framework, Rails are its dependent gems (jar file equivalent in ruby world).
Example Web Application Creation
Follow below steps to create a small blog application with CRUD (Create, Read, Update and Delete) operations.
Language : Ruby
Server : Mongrel
Database : MySQL
Steps
* WebApp Creation
o Login to Server
o Create blog WebApplication
Execute:
[S171015@ctsinnvlvmft1 ~]$ rails blog
Output:
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create config/initializers
create config/locales
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create test/fixtures
create test/functional
create test/integration
create test/performance
create test/unit
create vendor
create vendor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cache
create tmp/pids
create Rakefile
create README
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create config/database.yml
create config/routes.rb
create config/locales/en.yml
create config/initializers/backtrace_silencers.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/new_rails_defaults.rb
create config/initializers/session_store.rb
create config/environment.rb
create config/boot.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/console
create script/dbconsole
create script/destroy
create script/generate
create script/runner
create script/server
create script/plugin
create script/performance/benchmarker
create script/performance/profiler
create test/test_helper.rb
create test/performance/browsing_test.rb
create public/404.html
create public/422.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create public/javascripts/application.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log
* Database Configuration
o Modify the config/database.yml configuration
development:
adapter: mysql
encoding: utf8
reconnect: false
database: blog_development
pool: 5
username: S171015
password: password-1
socket: /var/lib/mysql/mysql.sock
test:
adapter: mysql
encoding: utf8
reconnect: false
database: blog_test
pool: 5
username: S171015
password: password-1
socket: /var/lib/mysql/mysql.sock
production:
adapter: mysql
encoding: utf8
reconnect: false
database: blog_production
pool: 5
username: S171015
password: password-1
socket: /var/lib/mysql/mysql.sock
* Create Database
[S171015@ctsinnvlvmft1 blog]$ rake db:create
Above command rake is general-purpose command-runner, which creates the database called 'blog_development.
* Create Resources
Execute:
[S171015@ctsinnvlvmft1 blog]$ script/generate scaffold Post name:string title:string content:text
Output:
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/posts
exists app/views/layouts/
exists test/functional/
exists test/unit/
create test/unit/helpers/
exists public/stylesheets/
create app/views/posts/index.html.erb
create app/views/posts/show.html.erb
create app/views/posts/new.html.erb
create app/views/posts/edit.html.erb
create app/views/layouts/posts.html.erb
create public/stylesheets/scaffold.css
create app/controllers/posts_controller.rb
create test/functional/posts_controller_test.rb
create app/helpers/posts_helper.rb
create test/unit/helpers/posts_helper_test.rb
route map.resources :posts
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/post.rb
create test/unit/post_test.rb
create test/fixtures/posts.yml
create db/migrate
create db/migrate/20090703064638_create_posts.rb
Command 'script/generate' used to generate files required by the rails, scaffold command will get you up and running quickly, it generates “one size fits all” code for quick development. In this example database table named 'Posts' will be created and associated MVC files for the same.
Controller File
file: app/controller/posts_controller.rb
class PostsController < ApplicationController
# GET /posts
# GET /posts.xml
def index # INDEX Action
...
end
# GET /posts/1
# GET /posts/1.xml
def show # Action to SHOW the post
...
end
# GET /posts/new
# GET /posts/new.xml
def new # Action to ADD new post
...
end
# GET /posts/1/edit
def edit # Action to EDIT post
...
end
# POST /posts
# POST /posts.xml
def create # Action to CREATE post
...
end
# PUT /posts/1
# PUT /posts/1.xml
def update # Action to UPDATE post
...
end
# DELETE /posts/1
# DELETE /posts/1.xml
def destroy # Action to DELETE post
@post = Post.find(params[:id])
...
end
Model File
file: app/models/post.rb
class Post < ActiveRecord::Base
...
end
View Files Contains minimal html code to render the content in the browser.
file: app/views/posts/edit.html.erb
file: app/views/posts/index.html.erb
file: app/views/posts/new.html.erb
file: app/views/posts/show.html.erb
Starting the Server
[S171015@ctsinnvlvmft1 blog]$ script/server
=> Booting Mongrel
=> Rails 2.3.2 application starting on http://localhost:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Access the Blog Application
http://localhost:3000/posts
Add new Post
Listing all Posts