Upload
toby-briggs
View
234
Download
4
Embed Size (px)
Citation preview
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• 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!!!
Hello World!!!
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, :contentbelongs_to :uservalidates :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
ModelActiveRecord::Base
DBMySQL,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 < ApplicationControllerrespond_to :html, :js, :json, :xml, :myCustomdef index@posts=Post.allrespond_with @postsend
def show@post=Post.find(params[:id])respond_with @postend
end
www.blog.com/posts
ROUTESget “/posts”,to:”posts#index”
ViewMake use of @posts
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<h1>All Posts</h1><%@posts.each do |post|%>
<h1><%=post.title%></h1><p><%=post.content%></p>
<%end%>
application.html.erb
<html><body><%=yield%></body>
</html>
ROUTESget “/posts”,to:”posts#index”
Controller#index action
ModelPost<ActiveRecord::Base
Viewapp/views/posts/index
Directory StructureBlog
appbinconfigdbliblogpublictesttmpvendor• .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
http://www.youtube.com/watch?v=UfALIVZc87c
• 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
http://www.youtube.com/watch?v=PQbuyKUaKFo
• 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
http://railscasts.com
Getting Started
http://guides.rubyonrails.org/getting_started.html
References
• http://rubyonrails.org/• http://guides.rubyonrails.org• http://railscasts.com• http://en.wikipedia.org/wiki/Ruby_on_Rails• http://youtube.com
QUESTIONS?