Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to use a form_for collection_select to display some select field options of account types.

Its occurred to me that it would be easier for the user if they could see the price of the type in each select option

this is my currently not-working code:

<%= a.collection_select :account_type, AccountType.all, :id, (:name+" - "+number_to_currency(:price)) %>

how can i concatenate the values so that (:name+" - "+number_to_currency(:price)) will actually work and not throw an error?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

See the documentation: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select

You can use the :text_method option to set the displayed text in the select dropdown.

In your AccountType model, define a method like this:

  def name_with_price
    "#{name} - $#{price}"
  end

Then, in your view, you can use:

<%= a.collection_select :account_type, nil, AccountType.all, :id, :name_with_price %>
share|improve this answer
    
i did see that in the documentation. but thats display logic in a model, which breaks the principles of MVC, right? –  Kristian May 3 '13 at 13:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.