Friday, November 24, 2006

select boxes with rails

This evening i started out making select boxes in rails. Data for first select box is coming directly from a table. it lists down all the options and includes a blank also. Rails has this convention of rendering param names as x[y] and you can access them in controller code using params[:x][:y]
<%=
@locations = Location.find(:all , :order=> "name")
collection_select(:location, :name, @locations, :name, :name,{ :include_blank => true }) %>

The name of select box would be location[name]. In some cases though we want the _TYPES_ coupled tightly with our model classes. in such cases , you can define options inside a class, like.

payment_types = [ ["check","check"], ["credit card" ,"cc"]].freeze

I found this on appendix c of agile web development with rails. To use this array we have to include

<%=
options = [ ["select a payment option", ""]] + Order::PAYMENT_TYPES
select ("order","pay_type",options, {:include_blank=>true}) %>
© Life of a third world developer
Maira Gall