SlideShare a Scribd company logo
1 of 41
Ruby && Rails && More
          a fast-paced intro




      Jean-Baptiste Feldis - @jbfeldis
@teacher                                   =
Teacher.where(

   :new => true,

   :motivated => true,

   :age => 27, # which *IS* still young

   :username => «JB»,

   :vouvoiement => «jamais»

)
@teacher.inspect
http://twitter.com/jbfeldis
  http://studiomelipone.eu
http://freshfromthehive.com

   http://upshotapp.com
       http://card.biz
   http://tumbulus.com
              ...
Todo List
1.Introduction ✔
2.Ruby
  2.1.Basics
  2.2.Syntax
  2.3.More than scripts

3.Rails
4.Everything else
5.The Project
Ruby
                               Basics




1. http://www.ruby-lang.org/
2. Full Object Oriented
3. Created by Yukihiro “Matz“ Matsumoto in 1995
4. “Ruby is designed for programmer productivity and fun,
  following the principles of good user interface design.”
Ruby
               Syntax


Switching to IRB (Interactive RuBy)
Ruby
                      More Than Scripts




• for the web > Rails / Sinatra / ...
• for apps > JRuby (Java) / IronRuby (.NET) / MacRuby (Obj-C)
Todo List
1.Introduction ✔
2.Ruby ✔
3.Rails
  3.1. Basics
  3.2. MVC
  3.3. Real life stuff

4.Everything else
5.The Project
Rails
                              Basics




1. http://rubyonrails.org/
2. Rails = web framework written in Ruby
3. Created by David Heinemeier Hansson (@dhh) from
  37signals’ Basecamp app
4. First release July 2004
5. For web developpers by web developpers
Rails
                        Basics - Philosophy




1. Opinionated
2. Convention over con guration
3. DRY (Don’t Repeat Yourself )
4. TDD (Test Driven Development)
Get Ready!
Rails
                      Hello World - Needs



Rails is composed of a few commands:
- rails (to build your apps)
- gem (a ruby package manager to extend your code)
- bundle (a cool tool to manage your gems)
- rake (ruby gem to launch tasks)

More than that we need:
- a database (sqlite by default, included)
- a VCS (let’s use Git instead of Subversion)
Rails
         Hello World - Rails




         > rails new hello

This command generates code for
    a new Rails web application 
  in a sub-directory called “hello”  
Rails
                     Hello World - The App Directory




• the whole rails app is in this one directory
 • no hidden con guration les in system directories
 • you will modify many of these les in the course of your development
 • if you use sqlite even the DB is in this directory
• in a dream world you could copy this to your server and go
  party with your friends
• feeling bad about a «hello» app? Just delete the directory and
  you’re done
Rails
                        Hello World - Environments




• rails is con gured with 3 environments (Rails.env):
 • development: on your local computer, no caching
 • test: env made to run tests
 • production: for your real servers, caching and more
• database con g is in con g/database.yml
• speci c env con g to be in con g/environments/
  development.rb, test.rb, production.rb
Rails
                   Hello World - One More Thing




• assets (like pictures, videos, .css, .js ...)
• static html les are in the public/ directory
• it’s the only directory exposed to the world, its content is
   delivered before executing the app code

• new rails app come with an index.html le, remove that one
  before launching your app (you can spend hours on that
  one)
Rails
   Hello World - Launch!




       > cd hello
    > bundle install
      > rails server
open http://localhost:3000
Rails
                              MVC




1. High level design pattern: helps you structure your app
2. Model = Representation of your real life resources
3. View = Interface user interacts with
4. Controller = Responsible for app logic (get stuff, change
  them, display the new state)
Takes Time And Efforts To
 Get «Why» This Is So Cool.
Believe Me, Though, You Will.
Rails
 MVC
Rails
                     MVC   - Scaffold




Rails helps you setup your app with scaffolds.
> rails g scaffold person rst_name:string last_name:string
> rake db:migrate
Rails
                               MVC       - Scaffold


Model
  app/models/person.rb
  db/migrate/20110512093227_create_people.rb

5 views
   app/views/people/index.html.erb
   app/views/people/show.html.erb
   app/views/people/new.html.erb
   app/views/people/edit.html.erb
   app/views/people/_form.html.erb

Controller
  app/controllers/people_controller.rb
  routes.rb: resources : people
Rails
 MVC
Rails
                                              MVC   - Model

Models inherit from ActiveRecord::Base.
Models are linked to database tables.

Gives us lots of amazing methods to nd...
People.all # nd all people in our database
People. rst # nd the rst record
People.where(:activated => true).limit(5).order(: rst_name) # nd 5 users ordered by rst name who are
   activated

To populate...
@p = People.new
@p. rst_name = Buce
@p.last_name = Wayne
@p.save # returns true if everything’s okay

To modify
@p. rst_name = Bruce
@p.save

Or to curate
@p.delete
Rails
                                                MVC          - More Model

Okay, amazing stuff including automatic accessors. What more?

Validations:
validates_presence_of :email
validates_uniqueness_of :email, :scope => :agency_id
validates_length_of :email, :within => 6..100, :allow_nil => true, :allow_blank => true

Relations:
belongs_to :plan
has_many :orders
has_many :promo_codes
has_one :gui_data

Callbacks:
before_create, after_save...

Scopes
scope :published, lambda {
   where("posts.published_at IS NOT NULL AND posts.published_at <= ?", Time.zone.now)
 }
 scope :published_since, lambda { |ago|
   published.where("posts.published_at >= ?", ago)
 }
 scope :recent, published.order("posts.published_at DESC")
Rails
                                                   MVC   - Views

Okay... So much to say...
display a variable in the code: <%= @var %>

First Name: <%= @person. rst_name %>

Content is escaped by default, if you need raw data use <%= raw @var %> or tell the view you data is
  html_safe:
<%= @var.html_safe %>

You have conditional and loop statements available:
<% if @person.active? %>
Cool to see you again.
<% else %>
Please activate your account.
<% end %>

<% @people.each do |p| %>
   First Name: <%= p. rst_name %>
<% end %>

Plus lots of helpers for forms, links, tags, text...
Rails
                  MVC   - Controller - Routes




abstract literal URLs from code

the mapping between URLs and code, serves two purposes:
  • recognize URLs and trigger a controller action
  • generate URLs from names or objects, so you don’t have
   to hard code them in views
Rails
                   MVC   - Controller - Routes




follow REST (REpresentional State Transfer) pattern, so HTTP
verb are used to «act» upon ressources:
GET - to fetch
POST - to create
PUT - to update
DELETE - to... delete
Rails
                            MVC        - Controller - Routes




 In routes.rb you have: resources :people
                > rake routes (remember this one)

people GET        /people(.:format)            {:action=>"index", :controller=>"people"}
people POST       /people(.:format)            {:action=>"create", :controller=>"people"}
new_person GET    /people/new(.:format)        {:action=>"new", :controller=>"people"}
edit_person GET   /people/:id/edit(.:format)   {:action=>"edit", :controller=>"people"}
person GET        /people/:id(.:format)        {:action=>"show", :controller=>"people"}
person PUT        /people/:id(.:format)        {:action=>"update", :controller=>"people"}
person DELETE     /people/:id(.:format)        {:action=>"destroy", :controller=>"people"}
Rails
                      MVC     - Controller - Routes




Rails generates us 7 actions:
• index: list of all resources
• show: get speci ed resource
• new: get a form to create a new resource
• create: post data to create the new resource
• edit: get a form to update speci ed resource
• update: put data to update speci ed resource
• delete: delete speci ed resource
Rails
         MVC   - Controller - Routes




Let’s have a look at our PeopleController.rb
Rails
    Real Life Stuff




(just to frighten you)
Todo List
1.Introduction ✔
2.Ruby ✔
3.Rails ✔
4.Everything else
  4.1. Gem
  4.2. Git
  4.3. Heroku

5.The Project
Everything Else
                            Gem


http://rubygems.org
helps you extend your code with great code (thanks to ruby
and the great community behind ruby/rails)
> gem list
> gem install paperclip
use bundler, in Gem le:
gem «paperclip»
then > bundle (which is equivalent to « > bundle install»)
Everything Else
                                     Git


http://git-scm.com and http://github.com
source version management IS TERRIBLY IMPORTANT
> git init .
> git status
> git add [ . | le | dir | -u ]
> git commit -v
> git branch new-branch
> git checkout new-branch
Git repo are local and can also be used remotely (in this case, you should
subscribe to Github)
Everything Else
                           Heroku



http://heroku.com
platform to deploy ruby apps «in the cloud»
> gem install heroku
> heroku create
> git push heroku master
> heroku logs
> heroku console
Todo List

1.Introduction ✔
2.Ruby ✔
3.Rails ✔
4.Everything else ✔
5.The Project
The Project


«It’s bloody hard to nd spare parts for your suit (or yourself ).
That would be so cool to have some kind of Amazon store to
                     get and sell them!»
                         Optimus Prime
The Project

      Create a marketplace to buy and sell spare parts for
             Transformers/Iron Man/Darth Vader.
• Create an account (buyer/seller)
• Post parts (price, quantity available...)
• Buy parts
• Administrate parts and users
The Project

Need to be online tomorrow (thank you Heroku). What you
will need (at least):
• Scaffolds
• Authentication (Devise)
• Roles (Cancan)
• File upload (Paperclip)
• Mails (Sendgrid)
For extra points: write tests (let’s say unit tests)
Thanks!
Questions?

More Related Content

What's hot

Distributed Ruby and Rails
Distributed Ruby and RailsDistributed Ruby and Rails
Distributed Ruby and Rails
Wen-Tien Chang
 
Fast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on OracleFast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on Oracle
Raimonds Simanovskis
 
Ruby on Rails Security
Ruby on Rails SecurityRuby on Rails Security
Ruby on Rails Security
amiable_indian
 
Introduction To Django (Strange Loop 2011)
Introduction To Django (Strange Loop 2011)Introduction To Django (Strange Loop 2011)
Introduction To Django (Strange Loop 2011)
Jacob Kaplan-Moss
 

What's hot (20)

Rails 4.0
Rails 4.0Rails 4.0
Rails 4.0
 
Pourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentPourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirent
 
REST APIs in Laravel 101
REST APIs in Laravel 101REST APIs in Laravel 101
REST APIs in Laravel 101
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
 
Distributed Ruby and Rails
Distributed Ruby and RailsDistributed Ruby and Rails
Distributed Ruby and Rails
 
Ruby on Rails introduction
Ruby on Rails introduction Ruby on Rails introduction
Ruby on Rails introduction
 
Fast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on OracleFast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on Oracle
 
Ruby on Rails Security
Ruby on Rails SecurityRuby on Rails Security
Ruby on Rails Security
 
An Introduction to Ruby on Rails
An Introduction to Ruby on RailsAn Introduction to Ruby on Rails
An Introduction to Ruby on Rails
 
Rails web api 开发
Rails web api 开发Rails web api 开发
Rails web api 开发
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weibo
 
Ruby On Rails Introduction
Ruby On Rails IntroductionRuby On Rails Introduction
Ruby On Rails Introduction
 
Finding Restfulness - Madrid.rb April 2014
Finding Restfulness - Madrid.rb April 2014Finding Restfulness - Madrid.rb April 2014
Finding Restfulness - Madrid.rb April 2014
 
Jasig Rubyon Rails
Jasig Rubyon RailsJasig Rubyon Rails
Jasig Rubyon Rails
 
JRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudJRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the Cloud
 
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory CourseRuby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
 
Rails 3 Beautiful Code
Rails 3 Beautiful CodeRails 3 Beautiful Code
Rails 3 Beautiful Code
 
Introduction To Django (Strange Loop 2011)
Introduction To Django (Strange Loop 2011)Introduction To Django (Strange Loop 2011)
Introduction To Django (Strange Loop 2011)
 
When To Use Ruby On Rails
When To Use Ruby On RailsWhen To Use Ruby On Rails
When To Use Ruby On Rails
 
Pi
PiPi
Pi
 

Similar to Supa fast Ruby + Rails

Jasig rubyon rails
Jasig rubyon railsJasig rubyon rails
Jasig rubyon rails
_zaMmer_
 
Jasig rubyon rails
Jasig rubyon railsJasig rubyon rails
Jasig rubyon rails
_zaMmer_
 
Learning to code for startup mvp session 3
Learning to code for startup mvp session 3Learning to code for startup mvp session 3
Learning to code for startup mvp session 3
Henry S
 
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Nilesh Panchal
 
Rails
RailsRails
Rails
SHC
 

Similar to Supa fast Ruby + Rails (20)

Dev streams2
Dev streams2Dev streams2
Dev streams2
 
Intro to Rails and MVC
Intro to Rails and MVCIntro to Rails and MVC
Intro to Rails and MVC
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRuby
 
Jasig rubyon rails
Jasig rubyon railsJasig rubyon rails
Jasig rubyon rails
 
Jasig rubyon rails
Jasig rubyon railsJasig rubyon rails
Jasig rubyon rails
 
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 survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developerRuby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developer
 
Learning to code for startup mvp session 3
Learning to code for startup mvp session 3Learning to code for startup mvp session 3
Learning to code for startup mvp session 3
 
Create a new project in ROR
Create a new project in RORCreate a new project in ROR
Create a new project in ROR
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Introduction To Ruby On Rails
Introduction To Ruby On RailsIntroduction To Ruby On Rails
Introduction To Ruby On Rails
 
12 Introduction to Rails
12 Introduction to Rails12 Introduction to Rails
12 Introduction to Rails
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
rails.html
rails.htmlrails.html
rails.html
 
rails.html
rails.htmlrails.html
rails.html
 
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - Keynote
 
Rails
RailsRails
Rails
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à Ruby
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

Supa fast Ruby + Rails

  • 1. Ruby && Rails && More a fast-paced intro Jean-Baptiste Feldis - @jbfeldis
  • 2. @teacher = Teacher.where( :new => true, :motivated => true, :age => 27, # which *IS* still young :username => «JB», :vouvoiement => «jamais» ) @teacher.inspect
  • 3. http://twitter.com/jbfeldis http://studiomelipone.eu http://freshfromthehive.com http://upshotapp.com http://card.biz http://tumbulus.com ...
  • 4. Todo List 1.Introduction ✔ 2.Ruby 2.1.Basics 2.2.Syntax 2.3.More than scripts 3.Rails 4.Everything else 5.The Project
  • 5. Ruby Basics 1. http://www.ruby-lang.org/ 2. Full Object Oriented 3. Created by Yukihiro “Matz“ Matsumoto in 1995 4. “Ruby is designed for programmer productivity and fun, following the principles of good user interface design.”
  • 6. Ruby Syntax Switching to IRB (Interactive RuBy)
  • 7. Ruby More Than Scripts • for the web > Rails / Sinatra / ... • for apps > JRuby (Java) / IronRuby (.NET) / MacRuby (Obj-C)
  • 8. Todo List 1.Introduction ✔ 2.Ruby ✔ 3.Rails 3.1. Basics 3.2. MVC 3.3. Real life stuff 4.Everything else 5.The Project
  • 9. Rails Basics 1. http://rubyonrails.org/ 2. Rails = web framework written in Ruby 3. Created by David Heinemeier Hansson (@dhh) from 37signals’ Basecamp app 4. First release July 2004 5. For web developpers by web developpers
  • 10. Rails Basics - Philosophy 1. Opinionated 2. Convention over con guration 3. DRY (Don’t Repeat Yourself ) 4. TDD (Test Driven Development)
  • 12. Rails Hello World - Needs Rails is composed of a few commands: - rails (to build your apps) - gem (a ruby package manager to extend your code) - bundle (a cool tool to manage your gems) - rake (ruby gem to launch tasks) More than that we need: - a database (sqlite by default, included) - a VCS (let’s use Git instead of Subversion)
  • 13. Rails Hello World - Rails > rails new hello This command generates code for a new Rails web application  in a sub-directory called “hello”  
  • 14. Rails Hello World - The App Directory • the whole rails app is in this one directory • no hidden con guration les in system directories • you will modify many of these les in the course of your development • if you use sqlite even the DB is in this directory • in a dream world you could copy this to your server and go party with your friends • feeling bad about a «hello» app? Just delete the directory and you’re done
  • 15. Rails Hello World - Environments • rails is con gured with 3 environments (Rails.env): • development: on your local computer, no caching • test: env made to run tests • production: for your real servers, caching and more • database con g is in con g/database.yml • speci c env con g to be in con g/environments/ development.rb, test.rb, production.rb
  • 16. Rails Hello World - One More Thing • assets (like pictures, videos, .css, .js ...) • static html les are in the public/ directory • it’s the only directory exposed to the world, its content is delivered before executing the app code • new rails app come with an index.html le, remove that one before launching your app (you can spend hours on that one)
  • 17. Rails Hello World - Launch! > cd hello > bundle install > rails server open http://localhost:3000
  • 18. Rails MVC 1. High level design pattern: helps you structure your app 2. Model = Representation of your real life resources 3. View = Interface user interacts with 4. Controller = Responsible for app logic (get stuff, change them, display the new state)
  • 19. Takes Time And Efforts To Get «Why» This Is So Cool. Believe Me, Though, You Will.
  • 21. Rails MVC - Scaffold Rails helps you setup your app with scaffolds. > rails g scaffold person rst_name:string last_name:string > rake db:migrate
  • 22. Rails MVC - Scaffold Model app/models/person.rb db/migrate/20110512093227_create_people.rb 5 views app/views/people/index.html.erb app/views/people/show.html.erb app/views/people/new.html.erb app/views/people/edit.html.erb app/views/people/_form.html.erb Controller app/controllers/people_controller.rb routes.rb: resources : people
  • 24. Rails MVC - Model Models inherit from ActiveRecord::Base. Models are linked to database tables. Gives us lots of amazing methods to nd... People.all # nd all people in our database People. rst # nd the rst record People.where(:activated => true).limit(5).order(: rst_name) # nd 5 users ordered by rst name who are activated To populate... @p = People.new @p. rst_name = Buce @p.last_name = Wayne @p.save # returns true if everything’s okay To modify @p. rst_name = Bruce @p.save Or to curate @p.delete
  • 25. Rails MVC - More Model Okay, amazing stuff including automatic accessors. What more? Validations: validates_presence_of :email validates_uniqueness_of :email, :scope => :agency_id validates_length_of :email, :within => 6..100, :allow_nil => true, :allow_blank => true Relations: belongs_to :plan has_many :orders has_many :promo_codes has_one :gui_data Callbacks: before_create, after_save... Scopes scope :published, lambda { where("posts.published_at IS NOT NULL AND posts.published_at <= ?", Time.zone.now) } scope :published_since, lambda { |ago| published.where("posts.published_at >= ?", ago) } scope :recent, published.order("posts.published_at DESC")
  • 26. Rails MVC - Views Okay... So much to say... display a variable in the code: <%= @var %> First Name: <%= @person. rst_name %> Content is escaped by default, if you need raw data use <%= raw @var %> or tell the view you data is html_safe: <%= @var.html_safe %> You have conditional and loop statements available: <% if @person.active? %> Cool to see you again. <% else %> Please activate your account. <% end %> <% @people.each do |p| %> First Name: <%= p. rst_name %> <% end %> Plus lots of helpers for forms, links, tags, text...
  • 27. Rails MVC - Controller - Routes abstract literal URLs from code the mapping between URLs and code, serves two purposes: • recognize URLs and trigger a controller action • generate URLs from names or objects, so you don’t have to hard code them in views
  • 28. Rails MVC - Controller - Routes follow REST (REpresentional State Transfer) pattern, so HTTP verb are used to «act» upon ressources: GET - to fetch POST - to create PUT - to update DELETE - to... delete
  • 29. Rails MVC - Controller - Routes In routes.rb you have: resources :people > rake routes (remember this one) people GET /people(.:format) {:action=>"index", :controller=>"people"} people POST /people(.:format) {:action=>"create", :controller=>"people"} new_person GET /people/new(.:format) {:action=>"new", :controller=>"people"} edit_person GET /people/:id/edit(.:format) {:action=>"edit", :controller=>"people"} person GET /people/:id(.:format) {:action=>"show", :controller=>"people"} person PUT /people/:id(.:format) {:action=>"update", :controller=>"people"} person DELETE /people/:id(.:format) {:action=>"destroy", :controller=>"people"}
  • 30. Rails MVC - Controller - Routes Rails generates us 7 actions: • index: list of all resources • show: get speci ed resource • new: get a form to create a new resource • create: post data to create the new resource • edit: get a form to update speci ed resource • update: put data to update speci ed resource • delete: delete speci ed resource
  • 31. Rails MVC - Controller - Routes Let’s have a look at our PeopleController.rb
  • 32. Rails Real Life Stuff (just to frighten you)
  • 33. Todo List 1.Introduction ✔ 2.Ruby ✔ 3.Rails ✔ 4.Everything else 4.1. Gem 4.2. Git 4.3. Heroku 5.The Project
  • 34. Everything Else Gem http://rubygems.org helps you extend your code with great code (thanks to ruby and the great community behind ruby/rails) > gem list > gem install paperclip use bundler, in Gem le: gem «paperclip» then > bundle (which is equivalent to « > bundle install»)
  • 35. Everything Else Git http://git-scm.com and http://github.com source version management IS TERRIBLY IMPORTANT > git init . > git status > git add [ . | le | dir | -u ] > git commit -v > git branch new-branch > git checkout new-branch Git repo are local and can also be used remotely (in this case, you should subscribe to Github)
  • 36. Everything Else Heroku http://heroku.com platform to deploy ruby apps «in the cloud» > gem install heroku > heroku create > git push heroku master > heroku logs > heroku console
  • 37. Todo List 1.Introduction ✔ 2.Ruby ✔ 3.Rails ✔ 4.Everything else ✔ 5.The Project
  • 38. The Project «It’s bloody hard to nd spare parts for your suit (or yourself ). That would be so cool to have some kind of Amazon store to get and sell them!» Optimus Prime
  • 39. The Project Create a marketplace to buy and sell spare parts for Transformers/Iron Man/Darth Vader. • Create an account (buyer/seller) • Post parts (price, quantity available...) • Buy parts • Administrate parts and users
  • 40. The Project Need to be online tomorrow (thank you Heroku). What you will need (at least): • Scaffolds • Authentication (Devise) • Roles (Cancan) • File upload (Paperclip) • Mails (Sendgrid) For extra points: write tests (let’s say unit tests)

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n