As realmadrid2727 indicates, changing to a select drop down box is quite easy, and the information about the syntax of the 'select' method is available on the RoR api page. Have a look here:
http://api.rubyonrails.com/classes/A...r.html#M001752
The problem is where do you get your list/array of options!
One way is to create a model to contain the items being selected and store them in the database. This is great if the list is large or being changed a lot. For example a list of products.
However, if the list is small and is unlikely to change (or you don't want it to change) I think there is a better way: define it in your model.
For example, in your set up I'd do this:
In the contract model I'd add:
Code:
NUMBERS_OF_PEOPLE = (0..15).to_a
def self.numbers_of_people
NUMBERS_OF_PEOPLE
end
In the controller method I'd add
Code:
@numbers_of_people = Contract.numbers_of_people
Then in the form partial:
Code:
<p><label for="contract_adults">Adults</label><br/>
<%= select 'contract', 'adults', @numbers_of_people.collect {|p| p,p} -%></p>
<p><label for="contract_children">Children</label><br/>
<%= select 'contract', 'children', @numbers_of_people.collect {|p| p,p} %></p>
The advantages of doing it this way is that there are:
- a single easy to find place where the numbers can be changed. You change it in one place and the whole application is updated automatically.
- The array of available options is made available to other input systems. All they have to do is look-up Contract.numbers_of_people.
- It makes the array of available options accessible to the model and therefore they can be used easily in model methods such as Contract.numbers_of_people:
In the Contract model:
Code:
validates_inclusion_of :children, :in => NUMBERS_OF_PEOPLE
validates_inclusion_of :adults, :in => NUMBERS_OF_PEOPLE
Bookmarks