Rails is a powerful framework for developing dynamic web applications. It is written in Ruby. Ruby is a must to write application back-end. It follows Model View Controller(MVC) to organize application. This app is to solve vertex coloring problem using Rubyonrails.
Ruby On Rails
Rails framework eases the creation of dynamic and rich web applications. It allows a developer to start from scratch or from inbuilt templates. Use scaffold to create various templates. Rails got a way of its own to develop applications. First we need to master it. If we wont read the manual, experiences with other frameworks and web development will lead us to trouble. Motto of rails to do more with very less effort.
MVC in rails
Splitting the application into MVC will isolate logic from view, code remains clean and will make it clear where different types of code belong for easier maintenance.
M – Model
Model represents the data and the rules to manipulate that data. Models are used for interaction with the database. i.e. One model for a table in our app. In rails interaction between application and database is achieved using active records.
Active records is an ORM(Object Relation Mapping) programming technique. ORM means mapping of objects between program and database for storage and processing. Actually rails uses an extension of active records. Unlike other mapping, we don’t have to specify database schema. Just edit a few lines in database configuration file like user, password, socket etc and also specify a migration file to make changes while updating. SQLite is the default database, but we can change it to MySql or whatever in need by changing the above two files.
V – View
It represents the user interface of application. They are HTML files with embedded ruby code .
C – Controllers
Controllers process the incoming requests from the web browser, communicate with models for data and pass that data to views for presentation.
- Install ruby from your distro repo
- gem from source tar
- install rails using gem.
Gem manages ruby packages and libraries. More detailed installation steps can be found at http://railstutorials.org/book/.
To start a new project
- $ rails new <project name>
- Use ‘bundle install’ to install all dependencies specified in Gemfile.
- $ rails generate controller home index
This will create a basic template to start a project. To test it, start server.
- $ rails server
starts server at http://localhost:3000. It shows a rail welcoming page.
To replace it with the new application index page follow these steps.
- remove public/index.html
- set default root route in /config/routes.rb
i.e. Uncomment the root :to => line and change it to “home#index”
The app folder holds the MVC files of the app. The controller have the controller file. It will have a default action, index. You can edit it to add new actions. To reflect addition of new actions, these should be added to to the routes.rb file. Each action can have methods. For each action, corresponding view should be placed in views folder. The URL is of the form http://localhost:3000/controller/action/method/.
Params can be used to receive values posted using POST. eg: to receive a parameter with name area use params[ :area ] or params[ “area” ]. The processed values can be embedded to the html page using <%= and %> tags. The control or loop expressions are used inside <% and %> and to use a value of a variable use <%= and %>.
Heroku provides a free hosting for rails applications. To use it, create a heroku account from http://heroku.com/. For installation do,
- $ sudo gem install heroku
- Now move to your git folder for rail app.
- $ heroku create
Give credentials when prompted and follow the link shown to go to your app page.
- $ git push heroku master
This will upload app.
Now go to the previous link to test application online.
Rails is a very good framework to develop web applications in a shot time and with less effort. The MVC model keeps the design simple and database can be used without worrying about schema. In built template system gives more time for user specific development. And using git and heroku, application can be loaded online. To check my code and its online presentation go to: