Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a drop down <select> field in a rails form?

I am creating a scaffold -

rails g scaffold Contact email:string email_provider:string  

but I want the email provider to be a drop down (with gmail/yahoo/msn as options) and not a text field. How can I do this ?

like image 296
iCyborg Avatar asked Jan 01 '13 18:01

iCyborg


2 Answers

You can take a look at the Rails documentation . Anyways , in your form :

  <%= f.collection_select :provider_id, Provider.order(:name),:id,:name, include_blank: true %> 

As you can guess , you should predefine email-providers in another model -Provider , to have where to select them from .

like image 129
R Milushev Avatar answered Sep 28 '22 03:09

R Milushev


Or for custom options

<%= f.select :desired_attribute, ['option1', 'option2']%> 
like image 27
Fdwillis Avatar answered Sep 28 '22 02:09

Fdwillis