RUBY ON RAILS (RoR) Ishwor Khadka
Why Ruby on Rails?
Contents Introduction MCV Directory Structure Environment Modes Test Driven Development Rails Philosophy Why RoR stands out? References Questions?
Introduction open-source, full-stack framework for developing database-backed web applications according to the Model-View-Control pattern. RESTful get post put delete David Heinemeier Hansson - founder David Heinemeier Hansson shipped in Mac OS X v10.5 Leopard for the first time written in ruby programming language
Ruby Complete object oriented language Simple Easy to write 3.times {puts “Hello World!!!”} Output: Hello World!!! Packages called gems Core gems in Rails 3.2 Actionmailer Actionpack Activerecord Activesource Activesupport Rails Rake
MCV Model-Controller-View Application pattern Divides application into 3 parts Model Controller View ModelView Controller
Data Facilitates Creation Use of data Validation of data Persistent storage Association belongs_to, has_many, has_and_belongs_to_many etc….. Implemented with ActiveRecord::Base Class Post < ActiveRecord::Base attr_accessible :title, :content belongs_to :user validates :title, presence: true, length:{minimum:5} end Post. Create(title: "My first post”,content:”This is my first post….”) Post.find(1) Post.find_by_title(“My first post”) Post.user.name => “John” Model ActiveRecord::Base DB MySQL,Sqlite,PostGre
Controller Middle man between Model and View Make sense of the http request Produce appropriate output Responsible for: Fetching and saving data to the model pass appropriate data to the view Class PostsController < ApplicationController respond_to :html, :js, :json, :xml, :myCustom def end def end ROUTES get “/posts”,to:”posts#index” View Make use
View What you see html.erb- embedded ruby Content inserted into main layout file Different html tags, forms, ajax helper Custom helper file Make use of data passed by controller E.g. view for posts#index All Posts application.html.erb
ROUTES get “/posts”,to:”posts#index” Controller #index action Model Post<ActiveRecord::Base View app/views/posts/index
Directory Structure Blog app bin config db lib log public test tmp vendor.gitignore config.ru Gemfile Gemfile.lock rakefile readme.rdoc Includes mcv part of application, assets and helper directory Configuration files Database schema, migration files List of gems used in the application
Environment Modes Three environments Test Development Production Different configuration file in /config directory test.rb development.rb production.rb Different database
Test Driven Development Test is written beforehand RoR promotes TDD Core testing gem included Tests file located in /tests directory Can use other testing gems RSpec Cucumber Useful in large projects
Rails Philosophy DRY-Don’t repeat yourself Convention over configuration Rails is opinionated
Why RoR stands out? Open source Rails philosophy Asset pipeline Compiles all the css, js files into one file respectively Decreases loading time Can be disabled if needed Large contributing rails community Very well documented Large number of gems available Video casts
Getting Started
References
QUESTIONS?