ruby trix
controller: How to search using MySQL with RoR
class SampleController < ApplicationController scaffold :sample sql="select name, job from table(test_pipe("+ params[:id] + "))" unless params[:id].nil? samples=Sample.find_by_sql(sql) end end
controller: How to search using dynamic finders
@changes = Change.find_all_by_project_id(@params["id"])
controller: Helpful helpers
helper :sort include SortHelper include ActionView::Helpers::TextHelper
controller + view: Use SortHelper2 to sort your tables
Download sort_helper.rb and put into app/helpers
controller:
helper :sort include SortHelper def list sort_init 'last_name' sort_update @items = Contact.find(:all, :order => sort_clause) end
view:
<thead> <tr> <%= sort_header_tag('id', :title => 'Sort by contact ID') %> <%= sort_header_tag('last_name', :caption => 'Name') %> <%= sort_header_tag('phone') %> <%= sort_header_tag('address', :width => 200) %> </tr> </thead>
view: Use TextHelper to zebra-stripe your table rows
your .rhtml file:
<%- for item in @items do -%> <tr class="<%= cycle("even", "odd") %>"> ... use item ... </tr> <%- end -%>
corresponding css in public/stylesheets/style.css:
tr.even { background: #E8E8E8 } tr.odd { background: white }
page revision: 0, last edited: 04 Feb 2008 06:18