Rails VUWIT workshop

806 views

Published on

My slides for introducing VUWIT members to rails.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
806
On SlideShare
0
From Embeds
0
Number of Embeds
50
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Rails VUWIT workshop

  1. 1. Me Breccan McLeod-Lundy Developer at 3months Recent projects include: consumer.org.nz, digitalnz.org and sponsafier.com
  2. 2. Ruby(is not rails) Variables i = 3 i * 4 etc
  3. 3. Iterating and blocks i = [1,2] i.each do |one_i| Something End i.each do { |i| something }
  4. 4. Classes class Hello def hi puts “hello” end end
  5. 5. Classes(cont) hello_object = Hello.new hello.hi # hello
  6. 6. If blocks if something stuff elsif something_else Other stuffe else Other other stuff end
  7. 7. Ranges and symbols (1...80) is a range contains everything within it and can be iterated over. Colons start symbols which are minimal bits of data representing only themselves, often used as keys in hashes. :symbol
  8. 8. Rails Started 2004 Included in Mac OS X by default in 2006 Recently: twitter, ravelry etc.
  9. 9. Opinionated Sensible Defaults Don't Repeat Yourself
  10. 11. Generators rails appname script/generate model/controller name script/generate migration migration
  11. 12. Scaffold generation Not really my first choice, but good for demonstrations and prototyping. script/generate scaffold Movie name:string description:text
  12. 13. Migrations Go up and down add_column :table, :name, type remove_column :table, :name
  13. 14. Routes Create restful routes for a controller map.resources :controller_name Make default page the index map.root :controller => 'movies', :action => 'index (may also need to delete index.html in public)
  14. 15. Adding Reviews script/generate scaffold Review rating:integer movie_id:integer content:text In movie model add has_many :reviews In review model add belongs_to :movie
  15. 16. Reviews New method in the controller needs an array of movies to be added to: @movies = Movies.all.collect { |movie| [movie.name, movie.id]} The view needs a select: <%= f.select(:movie, @movies) %>
  16. 17. Partials Major part of reusing code. In a view: <% render :partial => “/reviews/form” %> Create partials with file names like _form.html.erb
  17. 18. Displaying reviews on a film In show method of the movie controller add: @reviews = @movie.reviews (After setting @movie) In the view <% @reviews.each do |review| %> <%= render :partial => “review”, :locals => { :review => review } %> <% end %> Create the partial or even extract it from the default review show!
  18. 19. Other resources Rails Guides Agile web dev with rails book Railscasts Wellrailed google group Google
  19. 20. Breccan McLeod-Lundy www.breccan.com [email_address] breccan on twitter, github etc

×