Ruby on Rails
 Workshop
   Brad Midgley
About me

• U of U grad
• U of Colorado at Colorado Springs
• Worked in Linux--Turbolinux
• Worked in Java--U of U
• Rails...
This is not a toy
Neither is Rails

• Programmers choose and enjoy it
• Huge collaboration benefit
• Open-source, freedom in license
• Inspir...
More Data Handling

• Using git
• Filtering at the model
• Sorting, driven by UI
• Filtering at the UI, search
Workshop up

• Project up-to-date
• Some data, user account
• Using/watching server log
• Launching console, interacting w...
Named scopes

• Spoiler?
• Enhancing the model
• Specify a subset of all rows
• Named scopes can be efficiently combined
Simple example
named_scope :family_friendly, :conditions => “rating in (‘G’, ‘PG’)”

Movie.family_friendly
Movie.family_fr...
Create your own

• Think of a subset of users or movies
• Implement in app/models/*.rb
• Test using console
• Combine filte...
Click-to-sort columns

• Very common to use ajax
• Often using a plugin
• A simple approach to start with
Non-ajax colsort

• http://garbageburrito.com/blog/entry/447/
  rails-super-cool-simple-column-sorting
• View renders an a...
Limitations?

• Doesn’t indicate sort order
• We’re not handling paging, will complicate it
• Let’s fix the first one
Sort order icon

• Poach up and down triangles
• No triangle if not sorting on this column
• Appropriate triangle for sort...
Search

• Search on title for now
• UI component
• Server filter component
A special form

• Normally forms post
• Search has no side effects
• Search should be bookmarkable
• Make it a get, re-use...
Form front

<% form_tag(movies_path, :method => "get") do %>
 <%= text_field_tag(:s) %>
 <%= submit_tag("Search", :name => ...
Form handler
• Watch params[:s]
• Try to keep existing code from today
• Remember we are passing a hash
• Implement this i...
Hints
Hints

•   :conditions => ['title like ?', "%#{params[:s]}%"]
Hints

•   :conditions => ['title like ?', "%#{params[:s]}%"]

•   overlay with :order => ...
Hints

•   :conditions => ['title like ?', "%#{params[:s]}%"]

•   overlay with :order => ...

•   Construct a hash with o...
Upcoming SlideShare
Loading in...5
×

URUG Ruby on Rails Workshop - Sesssion 5

524

Published on

Session 5 of the URUG Free Ruby on Rails workshop. This is the second tutorial session.

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
524
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

URUG Ruby on Rails Workshop - Sesssion 5

  1. 1. Ruby on Rails Workshop Brad Midgley
  2. 2. About me • U of U grad • U of Colorado at Colorado Springs • Worked in Linux--Turbolinux • Worked in Java--U of U • Rails/iPhone--Surgeworks
  3. 3. This is not a toy
  4. 4. Neither is Rails • Programmers choose and enjoy it • Huge collaboration benefit • Open-source, freedom in license • Inspiring other frameworks/languages
  5. 5. More Data Handling • Using git • Filtering at the model • Sorting, driven by UI • Filtering at the UI, search
  6. 6. Workshop up • Project up-to-date • Some data, user account • Using/watching server log • Launching console, interacting with app
  7. 7. Named scopes • Spoiler? • Enhancing the model • Specify a subset of all rows • Named scopes can be efficiently combined
  8. 8. Simple example named_scope :family_friendly, :conditions => “rating in (‘G’, ‘PG’)” Movie.family_friendly Movie.family_friendly.all(:order => :genre) Movie.family_friendly.all(:conditions => {:genre => 'Comedy'})
  9. 9. Create your own • Think of a subset of users or movies • Implement in app/models/*.rb • Test using console • Combine filters
  10. 10. Click-to-sort columns • Very common to use ajax • Often using a plugin • A simple approach to start with
  11. 11. Non-ajax colsort • http://garbageburrito.com/blog/entry/447/ rails-super-cool-simple-column-sorting • View renders an action with parameters to initiate sort • Model watches for the parameters
  12. 12. Limitations? • Doesn’t indicate sort order • We’re not handling paging, will complicate it • Let’s fix the first one
  13. 13. Sort order icon • Poach up and down triangles • No triangle if not sorting on this column • Appropriate triangle for sort order • Where does this code go?
  14. 14. Search • Search on title for now • UI component • Server filter component
  15. 15. A special form • Normally forms post • Search has no side effects • Search should be bookmarkable • Make it a get, re-use the action we are on
  16. 16. Form front <% form_tag(movies_path, :method => "get") do %> <%= text_field_tag(:s) %> <%= submit_tag("Search", :name => nil) %> <% end %>
  17. 17. Form handler • Watch params[:s] • Try to keep existing code from today • Remember we are passing a hash • Implement this in controllers/ movie_controller.rb • Modify the result set in @movies • GO
  18. 18. Hints
  19. 19. Hints • :conditions => ['title like ?', "%#{params[:s]}%"]
  20. 20. Hints • :conditions => ['title like ?', "%#{params[:s]}%"] • overlay with :order => ...
  21. 21. Hints • :conditions => ['title like ?', "%#{params[:s]}%"] • overlay with :order => ... • Construct a hash with only the :order and optionally add the :conditions => ...
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×