Rails 3 Beginner to Builder 2011 Week 5
Upcoming SlideShare
Loading in...5
×
 

Rails 3 Beginner to Builder 2011 Week 5

on

  • 5,015 views

This is the 5th of 8 presentations given at University of Texas during my Beginner to Builder Rails 3 Class. For more info and the whole series including video presentations at my blog: ...

This is the 5th of 8 presentations given at University of Texas during my Beginner to Builder Rails 3 Class. For more info and the whole series including video presentations at my blog:


http://schneems.com/tagged/Rails-3-beginner-to-builder-2011

Statistics

Views

Total Views
5,015
Views on SlideShare
1,673
Embed Views
3,342

Actions

Likes
1
Downloads
93
Comments
0

11 Embeds 3,342

http://schneems.com 3296
http://teamco-anthill.blogspot.com 12
http://localhost 10
http://www.helpified.com 8
http://www.slideshare.net 4
http://teamco-anthill.blogspot.de 4
http://www.schneems.com 2
https://helpified.com 2
url_unknown 2
http://teamco-anthill.blogspot.co.uk 1
http://teamco-anthill.blogspot.jp 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Rails 3 Beginner to Builder 2011 Week 5 Rails 3 Beginner to Builder 2011 Week 5 Presentation Transcript

  • Beginner to Builder Week 5July, 2011Friday, July 8, 2011
  • Reminder • Ethan Waldo • @EthanWaldo • Here to help answer questions • Say “hi”@SchneemsFriday, July 8, 2011
  • Rails - Week 5 • Data Flow • View to Controller • Routes • Params • Authenticating Users • Cryptographic Hashes (cool huh) • Authlogic@SchneemsFriday, July 8, 2011 View slide
  • RESTful REpresentational State Transfer • The state of the message matters • Different state = different message “You Again?” “You Again?”@SchneemsFriday, July 8, 2011 View slide
  • RESTful REpresentational State Transfer • Rails Maps Actions to HTTP Methods • GET - index, show, new • PUT - update • POST - create • DELETE - destroy@SchneemsFriday, July 8, 2011
  • Ruby convention • Documentation • ClassName#MethodName class Dog def show ... end end • Dog#show@SchneemsFriday, 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@SchneemsFriday, July 8, 2011
  • path helpers • _path • _url <%= dogs_path #=> "/dogs" %> <%= dogs_url #=> "http://localhost:3000/dogs" %>@SchneemsFriday, 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"}@SchneemsFriday, July 8, 2011
  • Routes Source: http://peepcode.com@SchneemsFriday, 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?@SchneemsFriday, 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?@SchneemsFriday, July 8, 2011
  • Routes • How do I define http://localhost:3000/ ? • Root of your application routes.rb root :to => "dogs#index"@SchneemsFriday, 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@SchneemsFriday, 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@SchneemsFriday, 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@SchneemsFriday, 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@SchneemsFriday, 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@SchneemsFriday, 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@SchneemsFriday, 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 ...@SchneemsFriday, 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@SchneemsFriday, 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 @dogsFriday, 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@SchneemsFriday, 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@SchneemsFriday, 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} )%>@SchneemsFriday, 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 %>@SchneemsFriday, 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@SchneemsFriday, 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 %>@SchneemsFriday, 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@SchneemsFriday, 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?@SchneemsFriday, 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@SchneemsFriday, July 8, 2011
  • Data Flow • How do I get data from browser to server? • Forms • form_for • form_tag • Links • Buttons@SchneemsFriday, 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@SchneemsFriday, July 8, 2011
  • Authenticating Users • Cryptographic Hashes • Authlogic@SchneemsFriday, 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@SchneemsFriday, July 8, 2011
  • Crypto Hashes • Different input • Different output ss ffPa myPass i myD A12D P29... 34U... != BG123@SchneemsFriday, July 8, 2011
  • Crypto Hashes • Same input • Same output ass myPass myP A12D 4U... ==A 34U... 12D3@SchneemsFriday, 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@SchneemsFriday, July 8, 2011
  • Crypto Hashes • Good for more than just users! • Comparing large datasets for equality • Authenticate downloaded files,@SchneemsFriday, 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@SchneemsFriday, July 8, 2011
  • Crypto Hashes • Are Awesome • Are Useful@SchneemsFriday, July 8, 2011
  • Authlogic • Authentication Gem • Don’t write your own authentication • Good for learning, but in production use a library gem install authlogic@SchneemsFriday, 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@SchneemsFriday, July 8, 2011
  • Routes • They’re kindof important (like, really really important)@SchneemsFriday, July 8, 2011
  • Questions? http://guides.rubyonrails.org http://stackoverflow.com http://peepcode.com@SchneemsFriday, July 8, 2011