18
RUBY ON RAILS (RoR) Ishwor Khad

RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

Embed Size (px)

Citation preview

Page 1: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

RUBY ON RAILS(RoR)

Ishwor Khadka

Page 2: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

Why Ruby on Rails?

Page 3: 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?

Page 4: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

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

Page 5: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

• 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

Page 6: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

MCV• Model-Controller-View• Application pattern• Divides application into 3 parts

• Model• Controller• View

ModelView

Controller

Page 7: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

• 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

Page 8: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

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

Page 9: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

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>

Page 10: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

ROUTESget “/posts”,to:”posts#index”

Controller#index action

ModelPost<ActiveRecord::Base

Viewapp/views/posts/index

Page 11: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

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

Page 12: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

Environment Modes

• Three environments• Test• Development• Production

• Different configuration file in /config directory• test.rb• development.rb• production.rb

• Different database

Page 13: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

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

Page 14: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

Rails Philosophy

• DRY-Don’t repeat yourself• Convention over configuration

http://www.youtube.com/watch?v=PQbuyKUaKFo

• Rails is opinionated

Page 15: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

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

Page 17: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

References

• http://rubyonrails.org/• http://guides.rubyonrails.org• http://railscasts.com• http://en.wikipedia.org/wiki/Ruby_on_Rails• http://youtube.com

Page 18: RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?

QUESTIONS?