Search code examples
formsruby-on-rails-4drop-down-menuscaffoldingselectlist

How to add a dropdown <option> in a rails form?


I had created a scaffold-

rails g scaffold shipment name:string description:text from:string to:string date:string pay:string

I want the form:string to be a drop down (with Kolkata/Delhi/Mumbai as options) and not a text field. How can I do this?

I had searched the web but everything was too upper level. I am new to Rails and am trying to learn this.


Solution

  • you can use the select helper in your app\views\shipments\_form.html.erb file

    <%= form_for(@shipment) do |f| %>
    
        <%= f.label :from %><br>
        <%= f.select :from, [ 'Kolkata','Delhi','Mumbai' ], :prompt => 'Select One' %>
    
        <%= f.submit %>
    <% end %>