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

前端 未结 8 1669
终归单人心
终归单人心 2020-12-02 06:23

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 (wi

相关标签:
8条回答
  • 2020-12-02 06:48

    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

    0 讨论(0)
  • 2020-12-02 06:49

    Or for custom options

    <%= f.select :desired_attribute, ['option1', 'option2']%>
    
    0 讨论(0)
  • 2020-12-02 06:52

    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 .

    0 讨论(0)
  • 2020-12-02 06:56

    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.

    0 讨论(0)
  • 2020-12-02 06:57

    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>
    
    0 讨论(0)
  • 2020-12-02 07:03

    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.

    0 讨论(0)
提交回复
热议问题