Question

How would one capitalise the first letter of a list of country's in below code example? My question is regarding simple_form, not a regular form for which one could use collection_select.

    = p.input :state,
              :readonly => "readonly",
              :collection => DataState.where(:country => "1"),
              :selected => 1,
              :id => "state",
              :name => "state",
              :prompt => "Please choose"

The problem is the list of states is in the database and saved like "alamaba","new york" I need it to be down cased in the database but would like to uppercase/capitalize the first letter for visual clarity.

Was it helpful?

Solution

Try this:

In your DataState model, add a method that displays a capitalized version of your state names

class DataState < ActiveRecord::Base
  def capitalized_state
    state.capitalize
  end
end

then use this new method to list your choices

Depending on if you're trying to save the id of the state as an association or just the string, the erb would be either

<%= f.input :state, collection: DataState.where(:country => "1").pluck(:capitalized_state) %>

or

<%= f.input :state, collection: DataState.where(:country => "1"), value_method: :id, label_method: :capitalized_state %>

where the former is for the state name and the latter is for the association id.

see this: using capitalize on a collection_select

Edit: I noticed that your html does not specify what attribute of the DataState class you're using as the text for your choices. If you're using rails, you should look into the collection_select form helper.

OTHER TIPS

One way of going about it, too, would be the addition of the text-transform: uppercase property of css, looking like this:

<% = f.input: state, collection: DataState.where (: country => "1"), style: 'text-transform: uppercase;' %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top