SlideShare une entreprise Scribd logo
1  sur  11
Télécharger pour lire hors ligne
Introduction to Ruby on Rails
Part 1 
• Type url and what’s next? 
• Routing 
• Controllers 
• Views 
• Models
Type url and what’s next? 
rorapp.com/products/983 
Routing 
• get ‚products’ => 
‚products#index’ 
• get ‚products/new’ => 
‚products#new’ 
• post ‚products' => 
‚products#create’ 
• get ‚products/:id’ => 
‚products#show’! 
• put ‚products/:id’ => 
‚products#update’ 
• delete ‚products/:id’ => 
‚products#destroy’ 
Controller
Type url and what’s next? 
Controller 
app/controllers/products_controller.rb 
! 
class ProductsController < ApplicactionController 
Routing View 
def show 
@product = Product.find(params[:id]) 
end 
! 
end 
Model
Type url and what’s next? 
View 
app/views/products/show.html.haml 
!. 
row 
.col-sm-6 
%h3= @product.name 
.col-sm-6 
%ul 
- for category in @product.categories 
%li= link_to category.name, category 
Controller
Type url and what’s next? 
View 
app/views/products/show.html.haml 
!. 
row 
.col-sm-6 
%h3= @product.name 
.col-sm-6 
%ul 
- for category in @product.categories 
%li= link_to category.name, category_path(category) 
Controller
Type url and what’s next? 
Model 
app/models/product_category_association.rb 
app/models/product.rb 
app/models/category.rb 
class ProductCategoryAssociation < ActiveRecord::Base 
belongs_to :category 
belongs_to :product 
end 
class Product < ActiveRecord::Base 
has_many :product_category_associations 
has_many :categories, :through 
=> :product_category_associations 
end Controller 
class Category < ActiveRecord::Base 
has_many :product_category_associations 
has_many :products, :through 
=> :product_category_associations 
end
Type url and what’s next? 
Model 
class Product < ActiveRecord::Base 
has_many :product_category_associations 
has_many :categories, :through 
=> :product_category_associations 
end 
Controller 
class ActiveRecord::Base 
! 
def self.where […] 
def self.find(id) […] object/exception 
def self.order(params) […] 
def self.new […] 
! 
def save […] => true/false 
def save! […] => true/exception 
def update_attributes […] true/false 
def update_attributes! […] => true/exception 
! 
end
Routing 
• get ‚products’ => ‚products#index’ 
• get ‚products/new’ => ‚products#new’ 
• post ‚products' => ‚products#create’ 
• get ‚products/:id’ => ‚products#show’ 
• put ‚products/:id’ => ‚products#update’ 
config/routes.rb 
• delete ‚products/:id’ => ‚products#destroy’
Routing 
• resources :products
Routing 
namespace :admin do 
resources :users 
end 
! 
namespace :manager do 
resources :products 
end 
! 
namespace :customer do 
resources :products do 
collection do 
get :bestsellers 
=> customer/products/bestsellers 
end 
member do 
post :add_to_cart 
=> customer/products/433/add_to_cart 
end 
end 
end 
config/routes.rb

Contenu connexe

Tendances (8)

Green Light for the Apps with Calaba.sh - DroidCon Paris 2014
Green Light for the Apps with Calaba.sh - DroidCon Paris 2014Green Light for the Apps with Calaba.sh - DroidCon Paris 2014
Green Light for the Apps with Calaba.sh - DroidCon Paris 2014
 
How to build testable UIs
How to build testable UIsHow to build testable UIs
How to build testable UIs
 
AngularDay 2018 - Angular Elements
AngularDay 2018 - Angular ElementsAngularDay 2018 - Angular Elements
AngularDay 2018 - Angular Elements
 
Presentation.Key
Presentation.KeyPresentation.Key
Presentation.Key
 
Behat, Test Driven Framework for BDD by Jeevan Bhushetty
Behat, Test Driven Framework for BDD by Jeevan BhushettyBehat, Test Driven Framework for BDD by Jeevan Bhushetty
Behat, Test Driven Framework for BDD by Jeevan Bhushetty
 
Coder Presentation
Coder  PresentationCoder  Presentation
Coder Presentation
 
Story Driven Web Development
Story Driven Web DevelopmentStory Driven Web Development
Story Driven Web Development
 
AJAX Solr JS filters in Drupal
AJAX Solr JS filters in DrupalAJAX Solr JS filters in Drupal
AJAX Solr JS filters in Drupal
 

Similaire à Introduction to Ruby On Rails

Resource and view
Resource and viewResource and view
Resource and view
Papp Laszlo
 
深入淺出 MVC
深入淺出 MVC深入淺出 MVC
深入淺出 MVC
Jace Ju
 
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Yasuko Ohba
 
20130528 solution linux_frousseau_nopain_webdev
20130528 solution linux_frousseau_nopain_webdev20130528 solution linux_frousseau_nopain_webdev
20130528 solution linux_frousseau_nopain_webdev
Frank Rousseau
 

Similaire à Introduction to Ruby On Rails (20)

Rails antipattern-public
Rails antipattern-publicRails antipattern-public
Rails antipattern-public
 
Rails antipatterns
Rails antipatternsRails antipatterns
Rails antipatterns
 
Ruby on Rails + AngularJS + Twitter Bootstrap
Ruby on Rails + AngularJS + Twitter BootstrapRuby on Rails + AngularJS + Twitter Bootstrap
Ruby on Rails + AngularJS + Twitter Bootstrap
 
Angular Classy
Angular ClassyAngular Classy
Angular Classy
 
Resource and view
Resource and viewResource and view
Resource and view
 
深入淺出 MVC
深入淺出 MVC深入淺出 MVC
深入淺出 MVC
 
Api development with rails
Api development with railsApi development with rails
Api development with rails
 
Rails for Beginners - Le Wagon
Rails for Beginners - Le WagonRails for Beginners - Le Wagon
Rails for Beginners - Le Wagon
 
Testing C# and ASP.net using Ruby
Testing C# and ASP.net using RubyTesting C# and ASP.net using Ruby
Testing C# and ASP.net using Ruby
 
Catalog display
Catalog displayCatalog display
Catalog display
 
Introduction to AngularJs
Introduction to AngularJsIntroduction to AngularJs
Introduction to AngularJs
 
Synapseindia reviews sharing intro cakephp
Synapseindia reviews sharing intro cakephpSynapseindia reviews sharing intro cakephp
Synapseindia reviews sharing intro cakephp
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial
 
The Rails Way
The Rails WayThe Rails Way
The Rails Way
 
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
 
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
 
mean stack
mean stackmean stack
mean stack
 
20130528 solution linux_frousseau_nopain_webdev
20130528 solution linux_frousseau_nopain_webdev20130528 solution linux_frousseau_nopain_webdev
20130528 solution linux_frousseau_nopain_webdev
 
Angularjs
AngularjsAngularjs
Angularjs
 
ParisJS #10 : RequireJS
ParisJS #10 : RequireJSParisJS #10 : RequireJS
ParisJS #10 : RequireJS
 

Dernier

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

Introduction to Ruby On Rails

  • 2. Part 1 • Type url and what’s next? • Routing • Controllers • Views • Models
  • 3. Type url and what’s next? rorapp.com/products/983 Routing • get ‚products’ => ‚products#index’ • get ‚products/new’ => ‚products#new’ • post ‚products' => ‚products#create’ • get ‚products/:id’ => ‚products#show’! • put ‚products/:id’ => ‚products#update’ • delete ‚products/:id’ => ‚products#destroy’ Controller
  • 4. Type url and what’s next? Controller app/controllers/products_controller.rb ! class ProductsController < ApplicactionController Routing View def show @product = Product.find(params[:id]) end ! end Model
  • 5. Type url and what’s next? View app/views/products/show.html.haml !. row .col-sm-6 %h3= @product.name .col-sm-6 %ul - for category in @product.categories %li= link_to category.name, category Controller
  • 6. Type url and what’s next? View app/views/products/show.html.haml !. row .col-sm-6 %h3= @product.name .col-sm-6 %ul - for category in @product.categories %li= link_to category.name, category_path(category) Controller
  • 7. Type url and what’s next? Model app/models/product_category_association.rb app/models/product.rb app/models/category.rb class ProductCategoryAssociation < ActiveRecord::Base belongs_to :category belongs_to :product end class Product < ActiveRecord::Base has_many :product_category_associations has_many :categories, :through => :product_category_associations end Controller class Category < ActiveRecord::Base has_many :product_category_associations has_many :products, :through => :product_category_associations end
  • 8. Type url and what’s next? Model class Product < ActiveRecord::Base has_many :product_category_associations has_many :categories, :through => :product_category_associations end Controller class ActiveRecord::Base ! def self.where […] def self.find(id) […] object/exception def self.order(params) […] def self.new […] ! def save […] => true/false def save! […] => true/exception def update_attributes […] true/false def update_attributes! […] => true/exception ! end
  • 9. Routing • get ‚products’ => ‚products#index’ • get ‚products/new’ => ‚products#new’ • post ‚products' => ‚products#create’ • get ‚products/:id’ => ‚products#show’ • put ‚products/:id’ => ‚products#update’ config/routes.rb • delete ‚products/:id’ => ‚products#destroy’
  • 11. Routing namespace :admin do resources :users end ! namespace :manager do resources :products end ! namespace :customer do resources :products do collection do get :bestsellers => customer/products/bestsellers end member do post :add_to_cart => customer/products/433/add_to_cart end end end config/routes.rb