SlideShare une entreprise Scribd logo
1  sur  45
Télécharger pour lire hors ligne
Beginner to Builder
                       Week 5




July, 2011
Friday, July 8, 2011
Reminder
                       • Ethan Waldo
                         • @EthanWaldo
                         • Here to help answer questions
                         • Say “hi”



@Schneems
Friday, July 8, 2011
Rails - Week 5
                        • Data Flow
                        • View to Controller
                          • Routes
                          • Params
                        • Authenticating Users
                          • Cryptographic Hashes (cool huh)
                          • Authlogic
@Schneems
Friday, July 8, 2011
RESTful
                                 REpresentational State Transfer
                       •   The state of the message matters
                           •   Different state = different message




                           “You Again?”               “You Again?”
@Schneems
Friday, July 8, 2011
RESTful
                              REpresentational State Transfer
                •      Rails Maps Actions to HTTP Methods
                       •   GET - index, show, new
                       •   PUT - update
                       •   POST - create
                       •   DELETE - destroy




@Schneems
Friday, July 8, 2011
Ruby convention
                        • Documentation
                         • ClassName#MethodName
                               class Dog
                                 def show
                                         ...
                                 end
                               end


                         • Dog#show
@Schneems
Friday, July 8, 2011
Routes
                                    routes.rb

                                     resources :dogs



                       • Routes
                        • Connect controller actions to URLs
                        • Example: /dogs/show/2
                          • Will call DogsController#show
                           • Pass params[:id] = 2
       resources sets up {index, new, create, destroy, edit, update} routes
@Schneems
Friday, July 8, 2011
path helpers
                       • _path
                       • _url
                         <%= dogs_path #=> "/dogs" %>

                         <%= dogs_url #=> "http://localhost:3000/dogs" %>




@Schneems
Friday, July 8, 2011
Routes
                       • routes.rb                                             routes.rb

                        • Specify resources                                     resources :dogs

                        • forget a route?
                          • run rake routes
           helper      Verb           Path                            Action               Controller

           dogs        GET      /dogs(.:format)            {:action=>"index", :controller=>"dogs"}
            dog        POST     /dogs(.:format)            {:action=>"create", :controller=>"dogs"}
        new_dog        GET      /dogs/new(.:format)        {:action=>"new", :controller=>"dogs"}
       edit_dog        GET      /dogs/:id/edit(.:format)   {:action=>"edit", :controller=>"dogs"}
            dog        GET      /dogs/:id(.:format)        {:action=>"show", :controller=>"dogs"}
                       PUT      /dogs/:id(.:format)        {:action=>"update", :controller=>"dogs"}
                       DELETE   /dogs/:id(.:format)        {:action=>"destroy", :controller=>"dogs"}


@Schneems
Friday, July 8, 2011
Routes




                            Source: http://peepcode.com



@Schneems
Friday, July 8, 2011
Routes
                        •   dog_path(@dog) (PUT)

                        •   dogs_path      (GET)

                        •   dog_path(@dog) (GET)

                        •   dog_path(@dog) (DELETE)

                        •   dogs_path      (POST)



          Name That Action!
          1.Find the Verb
          2.Plural or Singular?
          3.object.id or no args?
@Schneems
Friday, July 8, 2011
Routes
                        •   dog_path(@dog) (PUT)      Update

                        •   dogs_path      (GET)      Index

                        •   dog_path(@dog) (GET)      Show

                        •   dog_path(@dog) (DELETE)   Destroy

                        •   dogs_path      (POST)     Create



          Name That Action!
          1.Find the Verb
          2.Plural or Singular?
          3.object.id or no args?
@Schneems
Friday, July 8, 2011
Routes
                       •   How do I define http://localhost:3000/ ?
                           •   Root of your application
                       routes.rb

                           root :to => "dogs#index"




@Schneems
Friday, July 8, 2011
Routes
                       • Custom route
                        •   when resources don’t do enough use “match”
                            •   Define custom helpers using :as =>
                        match '/foobar/' => 'foo#search', :as => :search

                            •   Use route in view as search_path




                        http://guides.rubyonrails.org/routing.html
@Schneems
Friday, July 8, 2011
New - Active Record
                       # New does not save the object
                       dog = Dog.new(:name => "fido")
                       dog.id
                       >> nil

                       dog.name
                       >> "fido"

                       dog.new_record?
                       >> true
                       # must manually call save
                       dog.save
                       >> true
                       dog.id
                       >> 1

@Schneems
Friday, July 8, 2011
Create - Active Record
                        # Create does save the object
                        dog = Dog.create(:name => "lassie")
                        dog.id
                        >> 1

                        dog.name
                        >> "lassie"
                        dog.new_record?
                        >> false




@Schneems
Friday, July 8, 2011
Data Flow
         • How do I get data from Server?
           • Controller to View
             • Instance Variables - @dog
         • How do I get data from browser to server?
           • View to Controller
             • forms, links, buttons
         def create                       <%= @dog.name %>
           @dog = Dog.create(params[...   ...
         end

@Schneems
Friday, July 8, 2011
Data Flow
         • View to Controller (modify @variable)
           • View has @variable which has ID and
             attributes
           • Pass @variable.id and new attributes to
             controller
           • Controller finds object by the ID
             • modifies attributes and saves data
     <%= form_for(@dog) do |f| %>   def create
     ...                              @dog = Dog.create(params[...
     <% end %>                      end
@Schneems
Friday, July 8, 2011
link_to
                        • Send data using links
                        @dog = Dog.find(:id => 2)

                        <%= link_to 'Some Action', @dog %>

                          • link_to generates a link
                           • Calls a Controller Method
                           • Passes data

@Schneems
Friday, July 8, 2011
link_to
                       • link_to can take a path directly
                         <%= link_to 'Link Text', “/dogs” %>

                                            or
                         <%= link_to 'Link Text', dogs_path %>

                        •   So can form_for, form_tag, button_to ...




@Schneems
Friday, July 8, 2011
link_to
                        • path object is not needed if using a
                          ruby
                               helper


                        @dog = Dog.new
                        <%= link_to 'Link Text', @dog %>
                        # => DogsController#new

                        @dog = Dog.where(:name => "fido")
                        <%= link_to 'Link Text', @dog %>
                        # => DogsController#show




@Schneems
Friday, July 8, 2011
link_to
                        • What data does the controller see ?
                            <%= link_to 'Link Text', dog_path(@dog) %>

                            def show
                              dog_id = params[:id]
                              Dog.where(:id => dog_id)
                              ...
                            end


                         • params returns a hash passed via
                           http request

@Schneems
                         • :id is the key passed from @dogs
Friday, July 8, 2011
link_to        def show
                                        dog_id = params[:id]
                                        Dog.where(:id => dog_id)
                                        ...
                                      end


                        • Why only pass ID?
                        • minimize data sent to and from server
                         • decouple data sent from object
                           • security & continuity
                         • http methods don’t natively accept
                           ruby objects
@Schneems
Friday, July 8, 2011
link_to
                        • Can I send other stuff besides ID?
                         • You betcha!
      <%= link_to "Link Text",           search_path(:foo => {:bar => 42} )%>


                         meaning_of_life = params[:foo][:bar]


                         • pass additional info into
                           view_helper arguments
                         • all data is stored in params
@Schneems
Friday, July 8, 2011
button_to
                        • like link_to except renders as a
                          button
                        • default HTTP for buttons method is
                          POST
      <%= button_to "Link Text",      search_path(:foo => {:bar => 42} )%>




@Schneems
Friday, July 8, 2011
form_for
                          • form_for - view_helper
                           • generates form for object
                                               <%= form_for(@dog) do |f| %>

                        Controller      View     <div class="field">
                                                     <%= f.label :fur_color %><br />
                       @dog = Dog.new
                                                     <%= f.text_field :fur_color %>
                                                 </div>
                       @dog.fur_color
                                               ...
                                                 <div class="actions">
                                                     <%= f.submit %>
                                                 </div>
                                               <% end %>

@Schneems
Friday, July 8, 2011
form_for
                         • form_for - view_helper
                          • Uses object’s current state for
                             submit path
                       Controller                   View

                 @dog = Dog.new            <%= form_for(@dog) do |f| %>
                                            <div class="field">
                                              <%= f.label :fur_color %><br />
                                              <%= f.text_field :fur_color %>
                                            </div>
                                          ...
      @dog is a new Dog,                    <div class="actions">
                                              <%= f.submit %>
        so the form will                    </div>
      default to calling the              <% end %>
         create action
@Schneems
Friday, July 8, 2011
form_tag
                       • form_tag - view_helper
                        • generates form with no object
                        • needs a path
         Routes
                       • Path is set in routes.rb
          match '/search/' => 'foo#search', :as => :search

                                  View   <% form_tag search_path do %>
                                           Search:
                                           <%= text_field_tag 'query' %>
                                           <%= submit_tag 'Go!!'%>
                                         <% end %>
@Schneems
Friday, July 8, 2011
Controller Methods
                            • Why create & new?
                             • New then Create
                  dogs_controller.rb           app/views/dogs/new.html.erb
                          def new            <%= form_for(@dog) do |f| %>
                            @dog = Dog.new   ...
                          end                <% end %>

                       dogs_controller.rb      app/views/dogs/create.html.erb

     def create
                                                     <%= @dog.name %>
       @dog = Dog.create(params[...
                                                     ...
     end

@Schneems
Friday, July 8, 2011
Controller Methods
                       • What if I want extra actions?
                        • Use Index for other stuff ( like
                          search)
                        • Create your own if you have to
                          def my_crazy_custom_method
                            puts "This is OK, but not desirable"
                          end


                       index, new, create, destroy, edit, & update not enough?


@Schneems
Friday, July 8, 2011
Controller Methods
       • What if I run out of methods
         • Already used index, new, create, destroy, edit, & update
         • Create a new controller !
           • DogRacesController
           • DogGroomerController
           • etc.
                 multiple controllers per heavily used models is normal
@Schneems
Friday, July 8, 2011
Data Flow
                       • How do I get data from browser to
                         server?
                         • Forms
                           • form_for
                           • form_tag
                         • Links
                         • Buttons

@Schneems
Friday, July 8, 2011
Recap
    • Lots of view helpers take data from view to
      controller
      • Pick the one that best suits your needs
    • Run out of Routes to use?
      • generate a new controller
    • Forget a route
      • Run: rake routes

@Schneems
Friday, July 8, 2011
Authenticating Users
                         • Cryptographic Hashes
                         • Authlogic




@Schneems
Friday, July 8, 2011
Crypto Hashes
             • A functionfixed length any input and
               returns a
                          that takes
                                      string




                                              Passwo
               • function is not reversible
               • minor changes in input



                                                    rds
                 • major changes in output         a12n2
                                                         912348...


             • Examples: MD5, SHA1, SHA256

@Schneems
Friday, July 8, 2011
Crypto Hashes
                        • Different input
                          • Different output




                                                                         ss
                                                                     ffPa
                           myPass




                                                                       i
                                                                   myD
                                    A12D
                                                          P29...
                                        34U...
                                                 != BG123




@Schneems
Friday, July 8, 2011
Crypto Hashes
                        • Same input
                          • Same output




                                                                 ass
                           myPass




                                                              myP
                                    A12D              4U...
                                             ==A
                                        34U...   12D3




@Schneems
Friday, July 8, 2011
Crypto Hashes
                   • How does this help with user
                     authentication?
                     • passwords shouldn’t be stored in a
                       database
                       • store crypto-hash instead
                   • The same input produce the same output
                   • Compare hashed password to stored hash

@Schneems
Friday, July 8, 2011
Crypto Hashes
                   • Good for more than just users!
                   • Comparing large datasets for equality
                     • Authenticate downloaded files,




@Schneems
Friday, July 8, 2011
Crypto Hashes
                       • Considerations
                           •   Collisions - happen
                           •   Rainbow tables - exist
                           •   Timing Attacks - are not impossible
                           •   Don’t use MD5
                       •   Helpful techniques
                           •   “salt” your hashed data
                           •   hash your Hash

@Schneems
Friday, July 8, 2011
Crypto Hashes
                       • Are Awesome
                       • Are Useful




@Schneems
Friday, July 8, 2011
Authlogic
                       •   Authentication Gem
                       • Don’t write your own authentication
                           •   Good for learning, but in production use a
                               library




                                      gem install authlogic




@Schneems
Friday, July 8, 2011
Authlogic
                                 class User < ActiveRecord::Base
                                   acts_as_authentic
                                 end

                           class UserSession < Authlogic::Session::Base

                           end

                       •   Very flexible, lightweight, and modular
                       •   Doesn’t generate code, examples are online

@Schneems
Friday, July 8, 2011
Routes
                       • They’re kindof important




                            (like, really really important)

@Schneems
Friday, July 8, 2011
Questions?
                       http://guides.rubyonrails.org
                        http://stackoverflow.com
                           http://peepcode.com


@Schneems
Friday, July 8, 2011

Contenu connexe

Tendances

NoSQL, Apache SOLR and Apache Hadoop
NoSQL, Apache SOLR and Apache HadoopNoSQL, Apache SOLR and Apache Hadoop
NoSQL, Apache SOLR and Apache HadoopDmitry Kan
 
Introduction to Drupal (7) Theming
Introduction to Drupal (7) ThemingIntroduction to Drupal (7) Theming
Introduction to Drupal (7) ThemingRobert Carr
 
Drupal 7 Theme System
Drupal 7 Theme SystemDrupal 7 Theme System
Drupal 7 Theme SystemPeter Arato
 
Grok Drupal (7) Theming - 2011 Feb update
Grok Drupal (7) Theming - 2011 Feb updateGrok Drupal (7) Theming - 2011 Feb update
Grok Drupal (7) Theming - 2011 Feb updateLaura Scott
 
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)Laura Scott
 
BDUG Responsive Web Theming - 7/23/12
BDUG Responsive Web Theming - 7/23/12BDUG Responsive Web Theming - 7/23/12
BDUG Responsive Web Theming - 7/23/12ucbdrupal
 

Tendances (8)

NoSQL, Apache SOLR and Apache Hadoop
NoSQL, Apache SOLR and Apache HadoopNoSQL, Apache SOLR and Apache Hadoop
NoSQL, Apache SOLR and Apache Hadoop
 
Introduction to Drupal (7) Theming
Introduction to Drupal (7) ThemingIntroduction to Drupal (7) Theming
Introduction to Drupal (7) Theming
 
Drupal 7 Theme System
Drupal 7 Theme SystemDrupal 7 Theme System
Drupal 7 Theme System
 
Grok Drupal (7) Theming - 2011 Feb update
Grok Drupal (7) Theming - 2011 Feb updateGrok Drupal (7) Theming - 2011 Feb update
Grok Drupal (7) Theming - 2011 Feb update
 
Best Practices For Centrally Governing Your Portal And Taxonomy Echo Techno...
Best Practices For Centrally Governing Your Portal And Taxonomy   Echo Techno...Best Practices For Centrally Governing Your Portal And Taxonomy   Echo Techno...
Best Practices For Centrally Governing Your Portal And Taxonomy Echo Techno...
 
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
 
A look at Drupal 7 Theming
A look at Drupal 7 ThemingA look at Drupal 7 Theming
A look at Drupal 7 Theming
 
BDUG Responsive Web Theming - 7/23/12
BDUG Responsive Web Theming - 7/23/12BDUG Responsive Web Theming - 7/23/12
BDUG Responsive Web Theming - 7/23/12
 

Similaire à Rails 3 Beginner to Builder 2011 Week 5

Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3Richard Schneeman
 
Rails 3 Beginner to Builder 2011 Week 8
Rails 3 Beginner to Builder 2011 Week 8Rails 3 Beginner to Builder 2011 Week 8
Rails 3 Beginner to Builder 2011 Week 8Richard Schneeman
 
잘 알려지지 않은 Php 코드 활용하기
잘 알려지지 않은 Php 코드 활용하기잘 알려지지 않은 Php 코드 활용하기
잘 알려지지 않은 Php 코드 활용하기형우 안
 
Rails ORM De-mystifying Active Record has_many
Rails ORM De-mystifying Active Record has_manyRails ORM De-mystifying Active Record has_many
Rails ORM De-mystifying Active Record has_manyBlazing Cloud
 
international PHP2011_ilia alshanetsky_Hidden Features of PHP
international PHP2011_ilia alshanetsky_Hidden Features of PHPinternational PHP2011_ilia alshanetsky_Hidden Features of PHP
international PHP2011_ilia alshanetsky_Hidden Features of PHPsmueller_sandsmedia
 
Brisk: more powerful Hadoop powered by Cassandra
Brisk: more powerful Hadoop powered by CassandraBrisk: more powerful Hadoop powered by Cassandra
Brisk: more powerful Hadoop powered by Cassandrajbellis
 
HTML5: Building for a Faster Web
HTML5: Building for a Faster WebHTML5: Building for a Faster Web
HTML5: Building for a Faster WebEric Bidelman
 
Rails 3 Beginner to Builder 2011 Week 2
Rails 3 Beginner to Builder 2011 Week 2Rails 3 Beginner to Builder 2011 Week 2
Rails 3 Beginner to Builder 2011 Week 2Richard Schneeman
 
MongoDB at Sailthru: Scaling and Schema Design
MongoDB at Sailthru: Scaling and Schema DesignMongoDB at Sailthru: Scaling and Schema Design
MongoDB at Sailthru: Scaling and Schema DesignDATAVERSITY
 
UPenn on Rails pt 1
UPenn on Rails pt 1UPenn on Rails pt 1
UPenn on Rails pt 1Mat Schaffer
 

Similaire à Rails 3 Beginner to Builder 2011 Week 5 (10)

Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3
 
Rails 3 Beginner to Builder 2011 Week 8
Rails 3 Beginner to Builder 2011 Week 8Rails 3 Beginner to Builder 2011 Week 8
Rails 3 Beginner to Builder 2011 Week 8
 
잘 알려지지 않은 Php 코드 활용하기
잘 알려지지 않은 Php 코드 활용하기잘 알려지지 않은 Php 코드 활용하기
잘 알려지지 않은 Php 코드 활용하기
 
Rails ORM De-mystifying Active Record has_many
Rails ORM De-mystifying Active Record has_manyRails ORM De-mystifying Active Record has_many
Rails ORM De-mystifying Active Record has_many
 
international PHP2011_ilia alshanetsky_Hidden Features of PHP
international PHP2011_ilia alshanetsky_Hidden Features of PHPinternational PHP2011_ilia alshanetsky_Hidden Features of PHP
international PHP2011_ilia alshanetsky_Hidden Features of PHP
 
Brisk: more powerful Hadoop powered by Cassandra
Brisk: more powerful Hadoop powered by CassandraBrisk: more powerful Hadoop powered by Cassandra
Brisk: more powerful Hadoop powered by Cassandra
 
HTML5: Building for a Faster Web
HTML5: Building for a Faster WebHTML5: Building for a Faster Web
HTML5: Building for a Faster Web
 
Rails 3 Beginner to Builder 2011 Week 2
Rails 3 Beginner to Builder 2011 Week 2Rails 3 Beginner to Builder 2011 Week 2
Rails 3 Beginner to Builder 2011 Week 2
 
MongoDB at Sailthru: Scaling and Schema Design
MongoDB at Sailthru: Scaling and Schema DesignMongoDB at Sailthru: Scaling and Schema Design
MongoDB at Sailthru: Scaling and Schema Design
 
UPenn on Rails pt 1
UPenn on Rails pt 1UPenn on Rails pt 1
UPenn on Rails pt 1
 

Plus de Richard Schneeman

Scaling the Web: Databases & NoSQL
Scaling the Web: Databases & NoSQLScaling the Web: Databases & NoSQL
Scaling the Web: Databases & NoSQLRichard Schneeman
 
Rails 3 Beginner to Builder 2011 Week 4
Rails 3 Beginner to Builder 2011 Week 4Rails 3 Beginner to Builder 2011 Week 4
Rails 3 Beginner to Builder 2011 Week 4Richard Schneeman
 

Plus de Richard Schneeman (6)

Scaling the Web: Databases & NoSQL
Scaling the Web: Databases & NoSQLScaling the Web: Databases & NoSQL
Scaling the Web: Databases & NoSQL
 
Rails 3 Beginner to Builder 2011 Week 4
Rails 3 Beginner to Builder 2011 Week 4Rails 3 Beginner to Builder 2011 Week 4
Rails 3 Beginner to Builder 2011 Week 4
 
Potential Friend Finder
Potential Friend FinderPotential Friend Finder
Potential Friend Finder
 
UT on Rails3 2010- Week 4
UT on Rails3 2010- Week 4 UT on Rails3 2010- Week 4
UT on Rails3 2010- Week 4
 
UT on Rails3 2010- Week 2
UT on Rails3 2010- Week 2UT on Rails3 2010- Week 2
UT on Rails3 2010- Week 2
 
UT on Rails3 2010- Week 1
UT on Rails3 2010- Week 1UT on Rails3 2010- Week 1
UT on Rails3 2010- Week 1
 

Dernier

Top 10 Makeup Brands in India for women
Top 10  Makeup Brands in India for womenTop 10  Makeup Brands in India for women
Top 10 Makeup Brands in India for womenAkshitaBhatt19
 
9892124323 Pooja Nehwal - Book Local Housewife call girls in Nalasopara at Ch...
9892124323 Pooja Nehwal - Book Local Housewife call girls in Nalasopara at Ch...9892124323 Pooja Nehwal - Book Local Housewife call girls in Nalasopara at Ch...
9892124323 Pooja Nehwal - Book Local Housewife call girls in Nalasopara at Ch...Pooja Nehwal
 
Call Girls in civil lines Delhi 8264348440 ✅ call girls ❤️
Call Girls in civil lines Delhi 8264348440 ✅ call girls ❤️Call Girls in civil lines Delhi 8264348440 ✅ call girls ❤️
Call Girls in civil lines Delhi 8264348440 ✅ call girls ❤️soniya singh
 
All Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort Service
All Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort ServiceAll Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort Service
All Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort ServiceApsara Of India
 
A TO Z INDIA Monthly Magazine - MAY 2024
A TO Z INDIA Monthly Magazine - MAY 2024A TO Z INDIA Monthly Magazine - MAY 2024
A TO Z INDIA Monthly Magazine - MAY 2024Indira Srivatsa
 
VIP Model Call Girls Buldhana Call ON 8617697112 Starting From 5K to 25K High...
VIP Model Call Girls Buldhana Call ON 8617697112 Starting From 5K to 25K High...VIP Model Call Girls Buldhana Call ON 8617697112 Starting From 5K to 25K High...
VIP Model Call Girls Buldhana Call ON 8617697112 Starting From 5K to 25K High...Nitya salvi
 
❤️Call Girls In Chandigarh 08168329307 Dera Bassi Zirakpur Panchkula Escort S...
❤️Call Girls In Chandigarh 08168329307 Dera Bassi Zirakpur Panchkula Escort S...❤️Call Girls In Chandigarh 08168329307 Dera Bassi Zirakpur Panchkula Escort S...
❤️Call Girls In Chandigarh 08168329307 Dera Bassi Zirakpur Panchkula Escort S...Apsara Of India
 
💕COD Call Girls In Kurukshetra 08168329307 Pehowa Escort Service
💕COD Call Girls In Kurukshetra 08168329307 Pehowa Escort Service💕COD Call Girls In Kurukshetra 08168329307 Pehowa Escort Service
💕COD Call Girls In Kurukshetra 08168329307 Pehowa Escort ServiceApsara Of India
 
Call Girls In Lajpat Nagar__ 8448079011 __Escort Service In Delhi
Call Girls In Lajpat Nagar__ 8448079011 __Escort Service In DelhiCall Girls In Lajpat Nagar__ 8448079011 __Escort Service In Delhi
Call Girls In Lajpat Nagar__ 8448079011 __Escort Service In DelhiRaviSingh594208
 
Call Now ☎ 8264348440 !! Call Girls in Govindpuri Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Govindpuri Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Govindpuri Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Govindpuri Escort Service Delhi N.C.R.soniya singh
 
💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...
💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...
💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...Apsara Of India
 
UDAIPUR CALL GIRLS 96O287O969 CALL GIRL IN UDAIPUR ESCORT SERVICE
UDAIPUR CALL GIRLS 96O287O969 CALL GIRL IN UDAIPUR ESCORT SERVICEUDAIPUR CALL GIRLS 96O287O969 CALL GIRL IN UDAIPUR ESCORT SERVICE
UDAIPUR CALL GIRLS 96O287O969 CALL GIRL IN UDAIPUR ESCORT SERVICEApsara Of India
 
High Class Call Girls in Bangalore 📱9136956627📱
High Class Call Girls in Bangalore 📱9136956627📱High Class Call Girls in Bangalore 📱9136956627📱
High Class Call Girls in Bangalore 📱9136956627📱Pinki Misra
 
Hi Profile Escorts In Udaipur 09602870969 Call Girls in Sobaghpura Bhopalpura
Hi Profile Escorts In Udaipur 09602870969 Call Girls in Sobaghpura BhopalpuraHi Profile Escorts In Udaipur 09602870969 Call Girls in Sobaghpura Bhopalpura
Hi Profile Escorts In Udaipur 09602870969 Call Girls in Sobaghpura BhopalpuraApsara Of India
 
💞Call Girls In Sonipat 08168329307 Sonipat Kundli GTK Bypass EsCoRt Service
💞Call Girls In Sonipat 08168329307 Sonipat Kundli GTK Bypass EsCoRt Service💞Call Girls In Sonipat 08168329307 Sonipat Kundli GTK Bypass EsCoRt Service
💞Call Girls In Sonipat 08168329307 Sonipat Kundli GTK Bypass EsCoRt ServiceApsara Of India
 
Jumeirah Call Girls Dubai Concupis O528786472 Dubai Call Girls In Bur Dubai N...
Jumeirah Call Girls Dubai Concupis O528786472 Dubai Call Girls In Bur Dubai N...Jumeirah Call Girls Dubai Concupis O528786472 Dubai Call Girls In Bur Dubai N...
Jumeirah Call Girls Dubai Concupis O528786472 Dubai Call Girls In Bur Dubai N...hf8803863
 
WhatsApp 📞 8448380779 ✅Call Girls In Bhangel Sector 102 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Bhangel Sector 102 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Bhangel Sector 102 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Bhangel Sector 102 ( Noida)Delhi Call girls
 
💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service
💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service
💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort ServiceApsara Of India
 
👉Chandigarh Call Girls 📞Book Now📞👉 9878799926 👉Zirakpur Call Girl Service No ...
👉Chandigarh Call Girls 📞Book Now📞👉 9878799926 👉Zirakpur Call Girl Service No ...👉Chandigarh Call Girls 📞Book Now📞👉 9878799926 👉Zirakpur Call Girl Service No ...
👉Chandigarh Call Girls 📞Book Now📞👉 9878799926 👉Zirakpur Call Girl Service No ...rajveerescorts2022
 
💞ROYAL💞 UDAIPUR ESCORTS Call 09602870969 CaLL GiRLS in UdAiPuR EsCoRt SeRvIcE💞
💞ROYAL💞 UDAIPUR ESCORTS Call 09602870969 CaLL GiRLS in UdAiPuR EsCoRt SeRvIcE💞💞ROYAL💞 UDAIPUR ESCORTS Call 09602870969 CaLL GiRLS in UdAiPuR EsCoRt SeRvIcE💞
💞ROYAL💞 UDAIPUR ESCORTS Call 09602870969 CaLL GiRLS in UdAiPuR EsCoRt SeRvIcE💞Apsara Of India
 

Dernier (20)

Top 10 Makeup Brands in India for women
Top 10  Makeup Brands in India for womenTop 10  Makeup Brands in India for women
Top 10 Makeup Brands in India for women
 
9892124323 Pooja Nehwal - Book Local Housewife call girls in Nalasopara at Ch...
9892124323 Pooja Nehwal - Book Local Housewife call girls in Nalasopara at Ch...9892124323 Pooja Nehwal - Book Local Housewife call girls in Nalasopara at Ch...
9892124323 Pooja Nehwal - Book Local Housewife call girls in Nalasopara at Ch...
 
Call Girls in civil lines Delhi 8264348440 ✅ call girls ❤️
Call Girls in civil lines Delhi 8264348440 ✅ call girls ❤️Call Girls in civil lines Delhi 8264348440 ✅ call girls ❤️
Call Girls in civil lines Delhi 8264348440 ✅ call girls ❤️
 
All Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort Service
All Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort ServiceAll Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort Service
All Hotel Karnal Call Girls 08168329307 Noor Mahal Karnal Escort Service
 
A TO Z INDIA Monthly Magazine - MAY 2024
A TO Z INDIA Monthly Magazine - MAY 2024A TO Z INDIA Monthly Magazine - MAY 2024
A TO Z INDIA Monthly Magazine - MAY 2024
 
VIP Model Call Girls Buldhana Call ON 8617697112 Starting From 5K to 25K High...
VIP Model Call Girls Buldhana Call ON 8617697112 Starting From 5K to 25K High...VIP Model Call Girls Buldhana Call ON 8617697112 Starting From 5K to 25K High...
VIP Model Call Girls Buldhana Call ON 8617697112 Starting From 5K to 25K High...
 
❤️Call Girls In Chandigarh 08168329307 Dera Bassi Zirakpur Panchkula Escort S...
❤️Call Girls In Chandigarh 08168329307 Dera Bassi Zirakpur Panchkula Escort S...❤️Call Girls In Chandigarh 08168329307 Dera Bassi Zirakpur Panchkula Escort S...
❤️Call Girls In Chandigarh 08168329307 Dera Bassi Zirakpur Panchkula Escort S...
 
💕COD Call Girls In Kurukshetra 08168329307 Pehowa Escort Service
💕COD Call Girls In Kurukshetra 08168329307 Pehowa Escort Service💕COD Call Girls In Kurukshetra 08168329307 Pehowa Escort Service
💕COD Call Girls In Kurukshetra 08168329307 Pehowa Escort Service
 
Call Girls In Lajpat Nagar__ 8448079011 __Escort Service In Delhi
Call Girls In Lajpat Nagar__ 8448079011 __Escort Service In DelhiCall Girls In Lajpat Nagar__ 8448079011 __Escort Service In Delhi
Call Girls In Lajpat Nagar__ 8448079011 __Escort Service In Delhi
 
Call Now ☎ 8264348440 !! Call Girls in Govindpuri Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Govindpuri Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Govindpuri Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Govindpuri Escort Service Delhi N.C.R.
 
💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...
💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...
💗📲09602870969💕-Royal Escorts in Udaipur Call Girls Service Udaipole-Fateh Sag...
 
UDAIPUR CALL GIRLS 96O287O969 CALL GIRL IN UDAIPUR ESCORT SERVICE
UDAIPUR CALL GIRLS 96O287O969 CALL GIRL IN UDAIPUR ESCORT SERVICEUDAIPUR CALL GIRLS 96O287O969 CALL GIRL IN UDAIPUR ESCORT SERVICE
UDAIPUR CALL GIRLS 96O287O969 CALL GIRL IN UDAIPUR ESCORT SERVICE
 
High Class Call Girls in Bangalore 📱9136956627📱
High Class Call Girls in Bangalore 📱9136956627📱High Class Call Girls in Bangalore 📱9136956627📱
High Class Call Girls in Bangalore 📱9136956627📱
 
Hi Profile Escorts In Udaipur 09602870969 Call Girls in Sobaghpura Bhopalpura
Hi Profile Escorts In Udaipur 09602870969 Call Girls in Sobaghpura BhopalpuraHi Profile Escorts In Udaipur 09602870969 Call Girls in Sobaghpura Bhopalpura
Hi Profile Escorts In Udaipur 09602870969 Call Girls in Sobaghpura Bhopalpura
 
💞Call Girls In Sonipat 08168329307 Sonipat Kundli GTK Bypass EsCoRt Service
💞Call Girls In Sonipat 08168329307 Sonipat Kundli GTK Bypass EsCoRt Service💞Call Girls In Sonipat 08168329307 Sonipat Kundli GTK Bypass EsCoRt Service
💞Call Girls In Sonipat 08168329307 Sonipat Kundli GTK Bypass EsCoRt Service
 
Jumeirah Call Girls Dubai Concupis O528786472 Dubai Call Girls In Bur Dubai N...
Jumeirah Call Girls Dubai Concupis O528786472 Dubai Call Girls In Bur Dubai N...Jumeirah Call Girls Dubai Concupis O528786472 Dubai Call Girls In Bur Dubai N...
Jumeirah Call Girls Dubai Concupis O528786472 Dubai Call Girls In Bur Dubai N...
 
WhatsApp 📞 8448380779 ✅Call Girls In Bhangel Sector 102 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Bhangel Sector 102 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Bhangel Sector 102 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Bhangel Sector 102 ( Noida)
 
💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service
💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service
💞Sexy Call Girls In Ambala 08168329307 Shahabad Call Girls Escort Service
 
👉Chandigarh Call Girls 📞Book Now📞👉 9878799926 👉Zirakpur Call Girl Service No ...
👉Chandigarh Call Girls 📞Book Now📞👉 9878799926 👉Zirakpur Call Girl Service No ...👉Chandigarh Call Girls 📞Book Now📞👉 9878799926 👉Zirakpur Call Girl Service No ...
👉Chandigarh Call Girls 📞Book Now📞👉 9878799926 👉Zirakpur Call Girl Service No ...
 
💞ROYAL💞 UDAIPUR ESCORTS Call 09602870969 CaLL GiRLS in UdAiPuR EsCoRt SeRvIcE💞
💞ROYAL💞 UDAIPUR ESCORTS Call 09602870969 CaLL GiRLS in UdAiPuR EsCoRt SeRvIcE💞💞ROYAL💞 UDAIPUR ESCORTS Call 09602870969 CaLL GiRLS in UdAiPuR EsCoRt SeRvIcE💞
💞ROYAL💞 UDAIPUR ESCORTS Call 09602870969 CaLL GiRLS in UdAiPuR EsCoRt SeRvIcE💞
 

Rails 3 Beginner to Builder 2011 Week 5

  • 1. Beginner to Builder Week 5 July, 2011 Friday, July 8, 2011
  • 2. Reminder • Ethan Waldo • @EthanWaldo • Here to help answer questions • Say “hi” @Schneems Friday, July 8, 2011
  • 3. Rails - Week 5 • Data Flow • View to Controller • Routes • Params • Authenticating Users • Cryptographic Hashes (cool huh) • Authlogic @Schneems Friday, July 8, 2011
  • 4. RESTful REpresentational State Transfer • The state of the message matters • Different state = different message “You Again?” “You Again?” @Schneems Friday, July 8, 2011
  • 5. RESTful REpresentational State Transfer • Rails Maps Actions to HTTP Methods • GET - index, show, new • PUT - update • POST - create • DELETE - destroy @Schneems Friday, July 8, 2011
  • 6. Ruby convention • Documentation • ClassName#MethodName class Dog def show ... end end • Dog#show @Schneems Friday, July 8, 2011
  • 7. Routes routes.rb resources :dogs • Routes • Connect controller actions to URLs • Example: /dogs/show/2 • Will call DogsController#show • Pass params[:id] = 2 resources sets up {index, new, create, destroy, edit, update} routes @Schneems Friday, July 8, 2011
  • 8. path helpers • _path • _url <%= dogs_path #=> "/dogs" %> <%= dogs_url #=> "http://localhost:3000/dogs" %> @Schneems Friday, July 8, 2011
  • 9. Routes • routes.rb routes.rb • Specify resources resources :dogs • forget a route? • run rake routes helper Verb Path Action Controller dogs GET /dogs(.:format) {:action=>"index", :controller=>"dogs"} dog POST /dogs(.:format) {:action=>"create", :controller=>"dogs"} new_dog GET /dogs/new(.:format) {:action=>"new", :controller=>"dogs"} edit_dog GET /dogs/:id/edit(.:format) {:action=>"edit", :controller=>"dogs"} dog GET /dogs/:id(.:format) {:action=>"show", :controller=>"dogs"} PUT /dogs/:id(.:format) {:action=>"update", :controller=>"dogs"} DELETE /dogs/:id(.:format) {:action=>"destroy", :controller=>"dogs"} @Schneems Friday, July 8, 2011
  • 10. Routes Source: http://peepcode.com @Schneems Friday, July 8, 2011
  • 11. Routes • dog_path(@dog) (PUT) • dogs_path (GET) • dog_path(@dog) (GET) • dog_path(@dog) (DELETE) • dogs_path (POST) Name That Action! 1.Find the Verb 2.Plural or Singular? 3.object.id or no args? @Schneems Friday, July 8, 2011
  • 12. Routes • dog_path(@dog) (PUT) Update • dogs_path (GET) Index • dog_path(@dog) (GET) Show • dog_path(@dog) (DELETE) Destroy • dogs_path (POST) Create Name That Action! 1.Find the Verb 2.Plural or Singular? 3.object.id or no args? @Schneems Friday, July 8, 2011
  • 13. Routes • How do I define http://localhost:3000/ ? • Root of your application routes.rb root :to => "dogs#index" @Schneems Friday, July 8, 2011
  • 14. Routes • Custom route • when resources don’t do enough use “match” • Define custom helpers using :as => match '/foobar/' => 'foo#search', :as => :search • Use route in view as search_path http://guides.rubyonrails.org/routing.html @Schneems Friday, July 8, 2011
  • 15. New - Active Record # New does not save the object dog = Dog.new(:name => "fido") dog.id >> nil dog.name >> "fido" dog.new_record? >> true # must manually call save dog.save >> true dog.id >> 1 @Schneems Friday, July 8, 2011
  • 16. Create - Active Record # Create does save the object dog = Dog.create(:name => "lassie") dog.id >> 1 dog.name >> "lassie" dog.new_record? >> false @Schneems Friday, July 8, 2011
  • 17. Data Flow • How do I get data from Server? • Controller to View • Instance Variables - @dog • How do I get data from browser to server? • View to Controller • forms, links, buttons def create <%= @dog.name %> @dog = Dog.create(params[... ... end @Schneems Friday, July 8, 2011
  • 18. Data Flow • View to Controller (modify @variable) • View has @variable which has ID and attributes • Pass @variable.id and new attributes to controller • Controller finds object by the ID • modifies attributes and saves data <%= form_for(@dog) do |f| %> def create ... @dog = Dog.create(params[... <% end %> end @Schneems Friday, July 8, 2011
  • 19. link_to • Send data using links @dog = Dog.find(:id => 2) <%= link_to 'Some Action', @dog %> • link_to generates a link • Calls a Controller Method • Passes data @Schneems Friday, July 8, 2011
  • 20. link_to • link_to can take a path directly <%= link_to 'Link Text', “/dogs” %> or <%= link_to 'Link Text', dogs_path %> • So can form_for, form_tag, button_to ... @Schneems Friday, July 8, 2011
  • 21. link_to • path object is not needed if using a ruby helper @dog = Dog.new <%= link_to 'Link Text', @dog %> # => DogsController#new @dog = Dog.where(:name => "fido") <%= link_to 'Link Text', @dog %> # => DogsController#show @Schneems Friday, July 8, 2011
  • 22. link_to • What data does the controller see ? <%= link_to 'Link Text', dog_path(@dog) %> def show dog_id = params[:id] Dog.where(:id => dog_id) ... end • params returns a hash passed via http request @Schneems • :id is the key passed from @dogs Friday, July 8, 2011
  • 23. link_to def show dog_id = params[:id] Dog.where(:id => dog_id) ... end • Why only pass ID? • minimize data sent to and from server • decouple data sent from object • security & continuity • http methods don’t natively accept ruby objects @Schneems Friday, July 8, 2011
  • 24. link_to • Can I send other stuff besides ID? • You betcha! <%= link_to "Link Text", search_path(:foo => {:bar => 42} )%> meaning_of_life = params[:foo][:bar] • pass additional info into view_helper arguments • all data is stored in params @Schneems Friday, July 8, 2011
  • 25. button_to • like link_to except renders as a button • default HTTP for buttons method is POST <%= button_to "Link Text", search_path(:foo => {:bar => 42} )%> @Schneems Friday, July 8, 2011
  • 26. form_for • form_for - view_helper • generates form for object <%= form_for(@dog) do |f| %> Controller View <div class="field"> <%= f.label :fur_color %><br /> @dog = Dog.new <%= f.text_field :fur_color %> </div> @dog.fur_color ... <div class="actions"> <%= f.submit %> </div> <% end %> @Schneems Friday, July 8, 2011
  • 27. form_for • form_for - view_helper • Uses object’s current state for submit path Controller View @dog = Dog.new <%= form_for(@dog) do |f| %> <div class="field"> <%= f.label :fur_color %><br /> <%= f.text_field :fur_color %> </div> ... @dog is a new Dog, <div class="actions"> <%= f.submit %> so the form will </div> default to calling the <% end %> create action @Schneems Friday, July 8, 2011
  • 28. form_tag • form_tag - view_helper • generates form with no object • needs a path Routes • Path is set in routes.rb match '/search/' => 'foo#search', :as => :search View <% form_tag search_path do %> Search: <%= text_field_tag 'query' %> <%= submit_tag 'Go!!'%> <% end %> @Schneems Friday, July 8, 2011
  • 29. Controller Methods • Why create & new? • New then Create dogs_controller.rb app/views/dogs/new.html.erb def new <%= form_for(@dog) do |f| %> @dog = Dog.new ... end <% end %> dogs_controller.rb app/views/dogs/create.html.erb def create <%= @dog.name %> @dog = Dog.create(params[... ... end @Schneems Friday, July 8, 2011
  • 30. Controller Methods • What if I want extra actions? • Use Index for other stuff ( like search) • Create your own if you have to def my_crazy_custom_method puts "This is OK, but not desirable" end index, new, create, destroy, edit, & update not enough? @Schneems Friday, July 8, 2011
  • 31. Controller Methods • What if I run out of methods • Already used index, new, create, destroy, edit, & update • Create a new controller ! • DogRacesController • DogGroomerController • etc. multiple controllers per heavily used models is normal @Schneems Friday, July 8, 2011
  • 32. Data Flow • How do I get data from browser to server? • Forms • form_for • form_tag • Links • Buttons @Schneems Friday, July 8, 2011
  • 33. Recap • Lots of view helpers take data from view to controller • Pick the one that best suits your needs • Run out of Routes to use? • generate a new controller • Forget a route • Run: rake routes @Schneems Friday, July 8, 2011
  • 34. Authenticating Users • Cryptographic Hashes • Authlogic @Schneems Friday, July 8, 2011
  • 35. Crypto Hashes • A functionfixed length any input and returns a that takes string Passwo • function is not reversible • minor changes in input rds • major changes in output a12n2 912348... • Examples: MD5, SHA1, SHA256 @Schneems Friday, July 8, 2011
  • 36. Crypto Hashes • Different input • Different output ss ffPa myPass i myD A12D P29... 34U... != BG123 @Schneems Friday, July 8, 2011
  • 37. Crypto Hashes • Same input • Same output ass myPass myP A12D 4U... ==A 34U... 12D3 @Schneems Friday, July 8, 2011
  • 38. Crypto Hashes • How does this help with user authentication? • passwords shouldn’t be stored in a database • store crypto-hash instead • The same input produce the same output • Compare hashed password to stored hash @Schneems Friday, July 8, 2011
  • 39. Crypto Hashes • Good for more than just users! • Comparing large datasets for equality • Authenticate downloaded files, @Schneems Friday, July 8, 2011
  • 40. Crypto Hashes • Considerations • Collisions - happen • Rainbow tables - exist • Timing Attacks - are not impossible • Don’t use MD5 • Helpful techniques • “salt” your hashed data • hash your Hash @Schneems Friday, July 8, 2011
  • 41. Crypto Hashes • Are Awesome • Are Useful @Schneems Friday, July 8, 2011
  • 42. Authlogic • Authentication Gem • Don’t write your own authentication • Good for learning, but in production use a library gem install authlogic @Schneems Friday, July 8, 2011
  • 43. Authlogic class User < ActiveRecord::Base acts_as_authentic end class UserSession < Authlogic::Session::Base end • Very flexible, lightweight, and modular • Doesn’t generate code, examples are online @Schneems Friday, July 8, 2011
  • 44. Routes • They’re kindof important (like, really really important) @Schneems Friday, July 8, 2011
  • 45. Questions? http://guides.rubyonrails.org http://stackoverflow.com http://peepcode.com @Schneems Friday, July 8, 2011