Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using capitalize on a collection_select

If this has been answered before I cannot find it.

I have the following:

= f.collection_select :sex_id, @sexes, :id, :name

and this in the controller:

@sexes = Sex.all

the sexes are all stored in lowercase, like this:

id|name
 1|steer
 2|heifer
 3|holstein

I need them to output with Capital First letters:

Steer
Heifer
Holstein

I tried:

= f.collection_select :sex_id, @sexes, :id, :name.capitalize
= f.collection_select :sex_id, @sexes, 'id', 'name'.capitalize

but they do not work, and I didn't really expect them to, but had to try them before posting this.

like image 679
Toby Joiner Avatar asked Nov 19 '10 18:11

Toby Joiner


1 Answers

collection_select calls a method on each object to get the text for the option value. You can add a new method in the model to get the right value:

def name_for_select
  name.capitalize
end

then in the view:

= f.collection_select :sex_id, @sexes, :id, :name_for_select
like image 106
zetetic Avatar answered Oct 14 '22 17:10

zetetic