SlideShare uma empresa Scribd logo
1 de 35
An Introduction to
Ruby on Rails
1
An Introduction to Ruby and Rails
Outline
What is Ruby?
What is Rails?
Rails overview.
Sample RoR application and also with Spree
gem.
2
An Introduction to Ruby and Rails
What is Ruby?
 Ruby is a pure object oriented programming language. It was
created on February 24, 1993 by Yukihiro Matsumoto of Japan.
Ruby is a general-purpose, interpreted programming language.
3
An Introduction to Ruby and Rails
What is Rails?
 Rails is a web application development framework written in
the Ruby language. It is designed to make programming web
applications easier by making assumptions about what every
developer needs to get started.
 Open Source (MIT license)
 Based on a existing application (Basecamp)
 Provides common needs:
- Routing, sessions
- Database storage
- Business logic
- Generate HTML/XML/CSS/Ajax
 It allows you to write less code while accomplishing more than
many other languages and frameworks.
4
An Introduction to Ruby and Rails
Rails Advantages
 Convention over configuration
 Don’t Repeat Yourself
 Object Relational Mapping
 Model View Controller
 Reuse of code
5
An Introduction to Ruby and Rails
Convention over Configuration
 Table and foreign key naming
- Tables are multiples (users, orders, …)
- Foreign key naming: user_id
 Default locations
- MVC, Test, Languages, Plugins
 Naming
- Class names: CamelCase
- Files: lowercase_underscored.rb
6
An Introduction to Ruby and Rails
Don’t repeat yourself
 Everything is defined in a single, unambiguous place
 Easier to find code
- Only need to look once
- Can stop looking when found
- Well defined places for most items
 Much easier to maintain code
- Faster to change
- Less inconsistency bugs
7
8An Introduction to Ruby and Rails
MVC
 Model
- Object relationships
(users, orders)
 Controller
- Business logic (perform
a payment)
 View
- Visual representation
(generate HTML/XML)
8
An Introduction to Ruby and Rails
Object Relational Mapping
- Easily stored and retrieved
from a database without writing
SQL statements directly
- Use with less overall database
access code
9
An Introduction to Ruby and Rails
Re-use of code
 Gems and plugins, more then 1300
- For authentication, pagination, testing, …
 Git allows easy forking and merging
10
An Introduction to Ruby and Rails
Rails Disadvantages
 Rails is inefficient
 Rails is hard to desploy
11
An Introduction to Ruby and Rails
Rails is inefficient
 Rails uses a lot of memory, up to 150MB per instance
 Requires more application servers
 Where are your development constrains?
12
An Introduction to Ruby and Rails
Rails is hard to deploy
 Harder than PHP, better with Passenger
 Lots of moving parts
- Rails, apps, gems, plugins
- Application server, webserver
 Deployment via scripts
- Via the Capistrano tool
- Easy to break something
 Deployment to multiple servers
13
An Introduction to Ruby and Rails
Quick Rails Demo:
 $ rails new RoRApp
$ cd RoRApp
(Use an Aptana studio IDE)
 We have 3 environments( in config/database.yml)
1.Development
2.Test
3.Production
14
 If we want to configure the database as mysql2(default database is
sqllite), open the file config/database.yml and modify the database
name and options.
development:
adapter: mysql2
encoding: utf8
database: db/development/dev
username: root
Password: '123456'
 And also we need to add myspl2 gem in Gemfile as
gem 'mysql2'
An Introduction to Ruby and Rails
Configuring the Database:
15
An Introduction to Ruby and Rails
Now we need to bundle update which installs the mysql2
gem for our application.
$ bundle update
Create a Database :
 Now we have our database is configured, it’s time to have Rails
create an empty database. We can do this by running a rake
command:
$ rake db:create
16
An Introduction to Ruby and Rails
1.rails s (or) (default port is 3000)
2.rails server (or)
3.rails s -p 4000
 Verify whether the rails app is working properly or not by browsing
http://localhost:3000
 Here the default page is rendering from public/index.html
17
18
An Introduction to Ruby and Rails
 If we want to display a user defined text or anything in our home
page, we need to create a controller and a view
$ rails generate controller home index
Rails will create several files, including
app/views/home/index.html.erb and
app/controllers/home_controller.rb
 To make this index file as the home page, 1st
we need to delete the
default public/index.html page
$ rm public/index.html
19
An Introduction to Ruby and Rails
 Now, we have to tell Rails where your actual home page is located.
For that open the file config/routes.rb and edit as
root :to => "home#index"
 Check whether our home page is rendering proper page or not, for
that we need to start the rails serve as
$ rails s
20
An Introduction to Ruby and Rails
Scaffolding
Rails scaffolding is a quick way to generate some of the major pieces
of an application. If you want to create the models, views, and
controllers for a new resource in a single operation, scaffolding is
the tool for the job.
Example:
Creating a Resource for sessionApp:
We can start by generating a scaffold for the Post resource: this will
represent a single blog posting.
$ rails generate scaffold Post name:string title:string content:text
21
An Introduction to Ruby and Rails
File Purpose
db/migrate/20120606184725_create_po
sts.rb
Migration to create the posts table in
your database (your name will include a
different timestamp)
app/models/post.rb The Post model
config/routes.rb Edited to include routing information for
posts
app/controllers/posts_controller.rb The Posts controller
app/views/posts/index.html.erb A view to display an index of all posts
app/views/posts/edit.html.erb A view to edit an existing post
app/views/posts/show.html.erb A view to display a single post
app/views/posts/new.html.erb A view to create a new post
22
An Introduction to Ruby and Rails
Running a Migration:
 Rails generate scaffold command create is a database migration.
Migrations are Ruby classes that are designed to make it simple to
create and modify database tables. Rails uses rake commands to
run migrations, and it’s possible to undo a migration ($ rake
db:migrate rollback) after it’s been applied to your database.
23
An Introduction to Ruby and Rails
 If we look in the db/migrate/20120606184725_create_posts.rb
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :name
t.string :title
t.text :content
t.timestamps
end
end
 At this point, we can use a rake command to run the migration:
$ rake db:migrate
24
An Introduction to Ruby and Rails
Rails will execute this migration command and tell you it created the
Posts table.
== CreatePosts: migrating
===================================================
=
-- create_table(:posts)
-> 0.0019s
== CreatePosts: migrated (0.0020s)
===========================================
25
An Introduction to Ruby and Rails
Adding a Link:
 We can add a link to the home page. Open
app/views/home/index.html.erb and modify it as follows:
<h1>Hello, Rails!</h1>
<%= link_to "New Post", posts_path %>
When we run the server it displays the home page as
26
An Introduction to Ruby and Rails
The Model:
 The model file, app/models/post.rb is
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title
end
 Active Record supplies a great deal of functionality to our Rails
models for free, including basic database CRUD (Create, Read,
Update, Destroy) operations, data validation
27
An Introduction to Ruby and Rails
Adding Some Validation:
 Rails includes methods to help you validate the data that you send to
models. Open the app/models/post.rb file and edit it:
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title
validates :name, :presence => true
validates :title, :presence => true,
:length => {:minimum => 5}
end
28
An Introduction to Ruby and Rails
Listing All Posts
 How the application is showing us the list of Posts.
 Open the file app/controllers/posts_controller.rb and look at the
index action:
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @posts }
end
end
29
An Introduction to Ruby and Rails
 The HTML format(format.html) looks for a view in
app/views/posts/ with a name that corresponds to the
action name(index). Rails makes all of the instance
variables from the action available to the view. Here’s
app/views/posts/index.html.erb:
30
An Introduction to Ruby and Rails
<h1>Listing posts</h1>
<table>
<tr> <th>Name</th>
<th>Title</th>
<th>Content</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',
:method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New post', new_post_path %>
31
Creating an App using spree:
 Spree is a full featured commerce platform written for the Ruby on
Rails framework. It is designed to make programming commerce
applications easier by making several assumptions about what most
developers needs to get started. Spree is a production ready store.
An Introduction to Ruby and Rails 32
 Now we are going to see how we create
e-commerce(spree) application.
$ rails new SpreeApp
$ cd SpreeApp
$ spree install
Would you like to install the default gateways? (yes/no) [yes] yes
Would you like to run the migrations? (yes/no) [yes] yes
Would you like to load the seed data? (yes/no) [yes] yes
Would you like to load the sample data? (yes/no) [yes] yes
Admin Email [spree@example.com]
Admin Password [spree123]
Would you like to precompile assets? (yes/no) [yes] yes
$ rails server
An Introduction to Ruby and Rails 33
An Introduction to Ruby and Rails 34
An Introduction to Ruby and Rails
THANK YOU!
35

Mais conteúdo relacionado

Mais procurados

Security Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSecurity Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSource Conference
 
Rails Engine Patterns
Rails Engine PatternsRails Engine Patterns
Rails Engine PatternsAndy Maleh
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Umair Amjad
 
Creating a modern web application using Symfony API Platform, ReactJS and Red...
Creating a modern web application using Symfony API Platform, ReactJS and Red...Creating a modern web application using Symfony API Platform, ReactJS and Red...
Creating a modern web application using Symfony API Platform, ReactJS and Red...Jesus Manuel Olivas
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - IntroductionVagmi Mudumbai
 
REST APIs in Laravel 101
REST APIs in Laravel 101REST APIs in Laravel 101
REST APIs in Laravel 101Samantha Geitz
 
Getting Distributed (With Ruby On Rails)
Getting Distributed (With Ruby On Rails)Getting Distributed (With Ruby On Rails)
Getting Distributed (With Ruby On Rails)martinbtt
 
Laravel Restful API and AngularJS
Laravel Restful API and AngularJSLaravel Restful API and AngularJS
Laravel Restful API and AngularJSBlake Newman
 
AngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro FrameworkAngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro FrameworkBackand Cohen
 
Ruby on Rails: Coding Guideline
Ruby on Rails: Coding GuidelineRuby on Rails: Coding Guideline
Ruby on Rails: Coding GuidelineNascenia IT
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Elena Kolevska
 
REST in practice with Symfony2
REST in practice with Symfony2REST in practice with Symfony2
REST in practice with Symfony2Daniel Londero
 
RESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP FrameworkRESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP FrameworkBo-Yi Wu
 
Rails web api 开发
Rails web api 开发Rails web api 开发
Rails web api 开发shaokun
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weiboshaokun
 
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswanivvaswani
 

Mais procurados (20)

Security Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSecurity Goodness with Ruby on Rails
Security Goodness with Ruby on Rails
 
Rails Engine Patterns
Rails Engine PatternsRails Engine Patterns
Rails Engine Patterns
 
Migration from Rails2 to Rails3
Migration from Rails2 to Rails3Migration from Rails2 to Rails3
Migration from Rails2 to Rails3
 
Laravel intake 37 all days
Laravel intake 37 all daysLaravel intake 37 all days
Laravel intake 37 all days
 
Creating a modern web application using Symfony API Platform, ReactJS and Red...
Creating a modern web application using Symfony API Platform, ReactJS and Red...Creating a modern web application using Symfony API Platform, ReactJS and Red...
Creating a modern web application using Symfony API Platform, ReactJS and Red...
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - Introduction
 
Javascript laravel's friend
Javascript laravel's friendJavascript laravel's friend
Javascript laravel's friend
 
REST APIs in Laravel 101
REST APIs in Laravel 101REST APIs in Laravel 101
REST APIs in Laravel 101
 
Getting Distributed (With Ruby On Rails)
Getting Distributed (With Ruby On Rails)Getting Distributed (With Ruby On Rails)
Getting Distributed (With Ruby On Rails)
 
Laravel Restful API and AngularJS
Laravel Restful API and AngularJSLaravel Restful API and AngularJS
Laravel Restful API and AngularJS
 
AngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro FrameworkAngularJS with Slim PHP Micro Framework
AngularJS with Slim PHP Micro Framework
 
Ruby on Rails: Coding Guideline
Ruby on Rails: Coding GuidelineRuby on Rails: Coding Guideline
Ruby on Rails: Coding Guideline
 
Rails 4.0
Rails 4.0Rails 4.0
Rails 4.0
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
 
REST in practice with Symfony2
REST in practice with Symfony2REST in practice with Symfony2
REST in practice with Symfony2
 
RESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP FrameworkRESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP Framework
 
Rails web api 开发
Rails web api 开发Rails web api 开发
Rails web api 开发
 
What Is Hobo ?
What Is Hobo ?What Is Hobo ?
What Is Hobo ?
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weibo
 
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
 

Semelhante a Ruby on Rails introduction

Ruby Rails Web Development
Ruby Rails Web DevelopmentRuby Rails Web Development
Ruby Rails Web DevelopmentSonia Simi
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Railsanides
 
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...Matt Gauger
 
Building Application with Ruby On Rails Framework
Building Application with Ruby On Rails FrameworkBuilding Application with Ruby On Rails Framework
Building Application with Ruby On Rails FrameworkEdureka!
 
Rails
RailsRails
RailsSHC
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorialsunniboy
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on RailsAlessandro DS
 
Intro to Rails and MVC
Intro to Rails and MVCIntro to Rails and MVC
Intro to Rails and MVCSarah Allen
 
Introduction To Ruby On Rails
Introduction To Ruby On RailsIntroduction To Ruby On Rails
Introduction To Ruby On RailsSteve Keener
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on RailsDelphiCon
 
Jasig Rubyon Rails
Jasig Rubyon RailsJasig Rubyon Rails
Jasig Rubyon RailsPaul Pajo
 

Semelhante a Ruby on Rails introduction (20)

Dev streams2
Dev streams2Dev streams2
Dev streams2
 
Ruby Rails Web Development
Ruby Rails Web DevelopmentRuby Rails Web Development
Ruby Rails Web Development
 
Aspose pdf
Aspose pdfAspose pdf
Aspose pdf
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Ruby on rails for beginers
Ruby on rails for beginersRuby on rails for beginers
Ruby on rails for beginers
 
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
What's new and great in Rails 3 - Matt Gauger - Milwaukee Ruby Users Group De...
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
 
Building Application with Ruby On Rails Framework
Building Application with Ruby On Rails FrameworkBuilding Application with Ruby On Rails Framework
Building Application with Ruby On Rails Framework
 
Rails
RailsRails
Rails
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorial
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Intro to Rails and MVC
Intro to Rails and MVCIntro to Rails and MVC
Intro to Rails and MVC
 
Rail3 intro 29th_sep_surendran
Rail3 intro 29th_sep_surendranRail3 intro 29th_sep_surendran
Rail3 intro 29th_sep_surendran
 
Introduction To Ruby On Rails
Introduction To Ruby On RailsIntroduction To Ruby On Rails
Introduction To Ruby On Rails
 
Ruby Rails Web Development.pdf
Ruby Rails Web Development.pdfRuby Rails Web Development.pdf
Ruby Rails Web Development.pdf
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
 
Ruby on Rails
Ruby on Rails Ruby on Rails
Ruby on Rails
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
 
Jasig Rubyon Rails
Jasig Rubyon RailsJasig Rubyon Rails
Jasig Rubyon Rails
 

Último

Unidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxUnidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxmibuzondetrabajo
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书rnrncn29
 
ETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxNIMMANAGANTI RAMAKRISHNA
 
IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119APNIC
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa494f574xmv
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predieusebiomeyer
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书zdzoqco
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxDyna Gilbert
 
TRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxTRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxAndrieCagasanAkio
 
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书rnrncn29
 
Company Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxCompany Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxMario
 

Último (11)

Unidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxUnidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptx
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
 
ETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptx
 
IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predi
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptx
 
TRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxTRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptx
 
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
 
Company Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxCompany Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptx
 

Ruby on Rails introduction

  • 2. An Introduction to Ruby and Rails Outline What is Ruby? What is Rails? Rails overview. Sample RoR application and also with Spree gem. 2
  • 3. An Introduction to Ruby and Rails What is Ruby?  Ruby is a pure object oriented programming language. It was created on February 24, 1993 by Yukihiro Matsumoto of Japan. Ruby is a general-purpose, interpreted programming language. 3
  • 4. An Introduction to Ruby and Rails What is Rails?  Rails is a web application development framework written in the Ruby language. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started.  Open Source (MIT license)  Based on a existing application (Basecamp)  Provides common needs: - Routing, sessions - Database storage - Business logic - Generate HTML/XML/CSS/Ajax  It allows you to write less code while accomplishing more than many other languages and frameworks. 4
  • 5. An Introduction to Ruby and Rails Rails Advantages  Convention over configuration  Don’t Repeat Yourself  Object Relational Mapping  Model View Controller  Reuse of code 5
  • 6. An Introduction to Ruby and Rails Convention over Configuration  Table and foreign key naming - Tables are multiples (users, orders, …) - Foreign key naming: user_id  Default locations - MVC, Test, Languages, Plugins  Naming - Class names: CamelCase - Files: lowercase_underscored.rb 6
  • 7. An Introduction to Ruby and Rails Don’t repeat yourself  Everything is defined in a single, unambiguous place  Easier to find code - Only need to look once - Can stop looking when found - Well defined places for most items  Much easier to maintain code - Faster to change - Less inconsistency bugs 7
  • 8. 8An Introduction to Ruby and Rails MVC  Model - Object relationships (users, orders)  Controller - Business logic (perform a payment)  View - Visual representation (generate HTML/XML) 8
  • 9. An Introduction to Ruby and Rails Object Relational Mapping - Easily stored and retrieved from a database without writing SQL statements directly - Use with less overall database access code 9
  • 10. An Introduction to Ruby and Rails Re-use of code  Gems and plugins, more then 1300 - For authentication, pagination, testing, …  Git allows easy forking and merging 10
  • 11. An Introduction to Ruby and Rails Rails Disadvantages  Rails is inefficient  Rails is hard to desploy 11
  • 12. An Introduction to Ruby and Rails Rails is inefficient  Rails uses a lot of memory, up to 150MB per instance  Requires more application servers  Where are your development constrains? 12
  • 13. An Introduction to Ruby and Rails Rails is hard to deploy  Harder than PHP, better with Passenger  Lots of moving parts - Rails, apps, gems, plugins - Application server, webserver  Deployment via scripts - Via the Capistrano tool - Easy to break something  Deployment to multiple servers 13
  • 14. An Introduction to Ruby and Rails Quick Rails Demo:  $ rails new RoRApp $ cd RoRApp (Use an Aptana studio IDE)  We have 3 environments( in config/database.yml) 1.Development 2.Test 3.Production 14
  • 15.  If we want to configure the database as mysql2(default database is sqllite), open the file config/database.yml and modify the database name and options. development: adapter: mysql2 encoding: utf8 database: db/development/dev username: root Password: '123456'  And also we need to add myspl2 gem in Gemfile as gem 'mysql2' An Introduction to Ruby and Rails Configuring the Database: 15
  • 16. An Introduction to Ruby and Rails Now we need to bundle update which installs the mysql2 gem for our application. $ bundle update Create a Database :  Now we have our database is configured, it’s time to have Rails create an empty database. We can do this by running a rake command: $ rake db:create 16
  • 17. An Introduction to Ruby and Rails 1.rails s (or) (default port is 3000) 2.rails server (or) 3.rails s -p 4000  Verify whether the rails app is working properly or not by browsing http://localhost:3000  Here the default page is rendering from public/index.html 17
  • 18. 18
  • 19. An Introduction to Ruby and Rails  If we want to display a user defined text or anything in our home page, we need to create a controller and a view $ rails generate controller home index Rails will create several files, including app/views/home/index.html.erb and app/controllers/home_controller.rb  To make this index file as the home page, 1st we need to delete the default public/index.html page $ rm public/index.html 19
  • 20. An Introduction to Ruby and Rails  Now, we have to tell Rails where your actual home page is located. For that open the file config/routes.rb and edit as root :to => "home#index"  Check whether our home page is rendering proper page or not, for that we need to start the rails serve as $ rails s 20
  • 21. An Introduction to Ruby and Rails Scaffolding Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job. Example: Creating a Resource for sessionApp: We can start by generating a scaffold for the Post resource: this will represent a single blog posting. $ rails generate scaffold Post name:string title:string content:text 21
  • 22. An Introduction to Ruby and Rails File Purpose db/migrate/20120606184725_create_po sts.rb Migration to create the posts table in your database (your name will include a different timestamp) app/models/post.rb The Post model config/routes.rb Edited to include routing information for posts app/controllers/posts_controller.rb The Posts controller app/views/posts/index.html.erb A view to display an index of all posts app/views/posts/edit.html.erb A view to edit an existing post app/views/posts/show.html.erb A view to display a single post app/views/posts/new.html.erb A view to create a new post 22
  • 23. An Introduction to Ruby and Rails Running a Migration:  Rails generate scaffold command create is a database migration. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it’s possible to undo a migration ($ rake db:migrate rollback) after it’s been applied to your database. 23
  • 24. An Introduction to Ruby and Rails  If we look in the db/migrate/20120606184725_create_posts.rb class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| t.string :name t.string :title t.text :content t.timestamps end end  At this point, we can use a rake command to run the migration: $ rake db:migrate 24
  • 25. An Introduction to Ruby and Rails Rails will execute this migration command and tell you it created the Posts table. == CreatePosts: migrating =================================================== = -- create_table(:posts) -> 0.0019s == CreatePosts: migrated (0.0020s) =========================================== 25
  • 26. An Introduction to Ruby and Rails Adding a Link:  We can add a link to the home page. Open app/views/home/index.html.erb and modify it as follows: <h1>Hello, Rails!</h1> <%= link_to "New Post", posts_path %> When we run the server it displays the home page as 26
  • 27. An Introduction to Ruby and Rails The Model:  The model file, app/models/post.rb is class Post < ActiveRecord::Base attr_accessible :content, :name, :title end  Active Record supplies a great deal of functionality to our Rails models for free, including basic database CRUD (Create, Read, Update, Destroy) operations, data validation 27
  • 28. An Introduction to Ruby and Rails Adding Some Validation:  Rails includes methods to help you validate the data that you send to models. Open the app/models/post.rb file and edit it: class Post < ActiveRecord::Base attr_accessible :content, :name, :title validates :name, :presence => true validates :title, :presence => true, :length => {:minimum => 5} end 28
  • 29. An Introduction to Ruby and Rails Listing All Posts  How the application is showing us the list of Posts.  Open the file app/controllers/posts_controller.rb and look at the index action: def index @posts = Post.all respond_to do |format| format.html # index.html.erb format.json { render :json => @posts } end end 29
  • 30. An Introduction to Ruby and Rails  The HTML format(format.html) looks for a view in app/views/posts/ with a name that corresponds to the action name(index). Rails makes all of the instance variables from the action available to the view. Here’s app/views/posts/index.html.erb: 30
  • 31. An Introduction to Ruby and Rails <h1>Listing posts</h1> <table> <tr> <th>Name</th> <th>Title</th> <th>Content</th> <th></th> <th></th> <th></th> </tr> <% @posts.each do |post| %> <tr> <td><%= post.name %></td> <td><%= post.title %></td> <td><%= post.content %></td> <td><%= link_to 'Show', post %></td> <td><%= link_to 'Edit', edit_post_path(post) %></td> <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td> </tr> <% end %> </table> <br /> <%= link_to 'New post', new_post_path %> 31
  • 32. Creating an App using spree:  Spree is a full featured commerce platform written for the Ruby on Rails framework. It is designed to make programming commerce applications easier by making several assumptions about what most developers needs to get started. Spree is a production ready store. An Introduction to Ruby and Rails 32
  • 33.  Now we are going to see how we create e-commerce(spree) application. $ rails new SpreeApp $ cd SpreeApp $ spree install Would you like to install the default gateways? (yes/no) [yes] yes Would you like to run the migrations? (yes/no) [yes] yes Would you like to load the seed data? (yes/no) [yes] yes Would you like to load the sample data? (yes/no) [yes] yes Admin Email [spree@example.com] Admin Password [spree123] Would you like to precompile assets? (yes/no) [yes] yes $ rails server An Introduction to Ruby and Rails 33
  • 34. An Introduction to Ruby and Rails 34
  • 35. An Introduction to Ruby and Rails THANK YOU! 35