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

Ruby on-RailsRuby on-Rails-3Drop Down-MenuInputSelectlist

Ruby on-Rails Problem Overview


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 ?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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 .

Solution 2 - Ruby on-Rails

Or for custom options

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

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

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:

    <%= f.label :provider %>
    <%= f.collection_select :provider_id, Provider.all, :id, :name %>

  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

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

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>

Solution 7 - Ruby on-Rails

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.

Solution 8 - Ruby on-Rails

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"}%>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniCyborgView Question on Stackoverflow
Solution 1 - Ruby on-RailsR MilushevView Answer on Stackoverflow
Solution 2 - Ruby on-RailsFdwillisView Answer on Stackoverflow
Solution 3 - Ruby on-RailsMichael DurrantView Answer on Stackoverflow
Solution 4 - Ruby on-RailsphobosView Answer on Stackoverflow
Solution 5 - Ruby on-RailsgsumkView Answer on Stackoverflow
Solution 6 - Ruby on-RailsRaghvendra ParasharView Answer on Stackoverflow
Solution 7 - Ruby on-RailskonyakView Answer on Stackoverflow
Solution 8 - Ruby on-RailssivamaniView Answer on Stackoverflow