ruby on rails - Form_for select field, concatenating data into the display string -


i trying use form_for collection_select display select field options of account types.

its occurred me easier user if see price of type in each select option

this not-working code:

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

how can concatenate values (:name+" - "+number_to_currency(:price)) work , not throw error?

see documentation: http://api.rubyonrails.org/classes/actionview/helpers/formoptionshelper.html#method-i-collection_select

you can use :text_method option set displayed text in select dropdown.

in accounttype model, define method this:

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

then, in view, can use:

<%= a.collection_select :account_type, nil, accounttype.all, :id, :name_with_price %> 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -