iCyborg
iCyborg

Reputation: 4728

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 ?

Upvotes: 86

Views: 220725

Answers (9)

stevec
stevec

Reputation: 52208

I wanted to display one thing (human readable) but store another (an integer id).

Small example

Here's a small example that helped:

<%= form.select(:attribute_name, {cat: 5, dog: 3} )%>

The {cat: 5, dog: 3} will display "cat" and "dog", but save 5 and 3.


Real world example

Here's the actual use case. It displays the names of sellers (that humans can read), but saves the sellers' id (an integer):

<div class="field">
  <%= form.label :seller_id %>
  <%= form.select :seller_id, seller_names_and_ids(), {include_blank: true}, {required: true, class: "form-control"} %>
</div>

And the helper is defined as:

def seller_names_and_ids
  # We want this to produce a hash of keys (the thing to display) and values (the thing to save, 
  # in thise case the seller_id integer)
  sellers = Seller.all
  h = {}
  sellers.each do |seller|
    thing_to_display = seller.name + " (" + seller.id.to_s + ")"
    thing_to_save_in_db = seller.id
    h.store(thing_to_display, thing_to_save_in_db) 
  end
  h
end

Upvotes: 0

sivamani
sivamani

Reputation: 535

Rails drop down using has_many association for article and category:

has_many :articles

belongs_to :category

<%= form.select :category_id,Category.all.pluck(:name,:id),{prompt:'select'},{class: "form-control"}%>

Upvotes: 7

gsumk
gsumk

Reputation: 891

<%= f.select :email_provider, ["gmail","yahoo","msn"]%>

Upvotes: 7

phobos
phobos

Reputation: 522

This is a long way round, but if you have not yet implemented then you can originally create your models this way. The method below describes altering an existing database.

1) Create a new model for the email providers:
$ rails g model provider name

2) This will create your model with a name string and timestamps. It also creates the migration which we need to add to the schema with:
$ rake db:migrate

3) Add a migration to add the providers ID into the Contact:
$ rails g migration AddProviderRefToContacts provider:references

4) Go over the migration file to check it look OK, and migrate that too:
$ rake db:migrate

5) Okay, now we have a provider_id, we no longer need the original email_provider string:
$ rails g migration RemoveEmailProviderFromContacts

6) Inside the migration file, add the change which will look something like:

class RemoveEmailProviderFromContacts < ActiveRecord::Migration
  def change
    remove_column :contacts, :email_provider
  end
end

7) Once that is done, migrate the change:
$ rake db:migrate

8) Let's take this moment to update our models:
Contact: belongs_to :provider
Provider: has_many :contacts

9) Then, we set up the drop down logic in the _form.html.erb partial in the views:

  <div class="field">
    <%= f.label :provider %><br>
    <%= f.collection_select :provider_id, Provider.all, :id, :name %>
  </div>

10) Finally, we need to add the provders themselves. One way top do that would be to use the seed file:

Provider.destroy_all

gmail = Provider.create!(name: "gmail")
yahoo = Provider.create!(name: "yahoo")
msn = Provider.create!(name: "msn")

$ rake db:seed

Upvotes: 11

Fdwillis
Fdwillis

Reputation: 1049

Or for custom options

<%= f.select :desired_attribute, ['option1', 'option2']%>

Upvotes: 63

konyak
konyak

Reputation: 11706

In your model,

class Contact
  self.email_providers = %w[Gmail Yahoo MSN]
  validates :email_provider, :inclusion => email_providers
end

In your form,

<%= f.select :email_provider, 
    options_for_select(Contact.email_providers, @contact.email_provider) %>

the second arg of the options_for_select will have any current email_provider selected.

Upvotes: 4

R Milushev
R Milushev

Reputation: 4315

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 .

Upvotes: 107

Raghvendra Parashar
Raghvendra Parashar

Reputation: 4053

Please have a look here

Either you can use rails tag Or use plain HTML tags

Rails tag

<%= select("Contact", "email_provider", Contact::PROVIDERS, {:include_blank => true}) %>

*above line of code would become HTML code(HTML Tag), find it below *

HTML tag

<select name="Contact[email_provider]">
  <option></option>
  <option>yahoo</option>
  <option>gmail</option>
  <option>msn</option>
</select>

Upvotes: 6

Michael Durrant
Michael Durrant

Reputation: 96454

You create the collection in the Contact controller -

app/controllers/contacts_controller.erb 

Adding

@providers = Provider.all.by_name

to the new, create and edit methods, using a scope for the by_name in the Provider model - app/models/provider.rb - for the ordering by name

scope by_name  order(:name)

Then in the view - app/views/contacts/_form.html.erb - you use

<%= f.collection_select :provider_id, @providers, :id, :name, include_blank: true %>

For rails forms, I also strongly recommend you look at a form builder like simple_form - https://github.com/plataformatec/simple_form - which will do all the heavy lifting.

Upvotes: 17

Related Questions