Michal
Michal

Reputation: 139

Rails - collection_select - populate with the values listed in a model

I have a model defined like that:

class Order < ActiveRecord::Base
  belongs_to :user

  TYPES = %w[t_01 t_02 t_03]
  validates :order_type, inclusion: { in: TYPES }
end

I am trying to make a dropdown menu in the view that will be populated by values available in TYPES.

The one shown below is of course not the right one, because it populates dropdown menu with types that belong to orders already recorded in DB:

<div class="field">
  <%= f.label :order_type %><br>
  <%= f.collection_select :order_type, Order.all, :order_type, :order_type %>
</div>

Can somebody give me any hint how I can sort it out? Thank you in advance.

Upvotes: 1

Views: 293

Answers (2)

Pushpalata
Pushpalata

Reputation: 7

You can also use this as

<%= f.collection_select :order_type, Order::TYPES , :to_s, :to_s, {include_blank: false}%>

Upvotes: 0

miler350
miler350

Reputation: 1421

#model

 def self.types
  TYPES
 end


 #view
 <%= f.collection_select :order_type, Order.types, :to_s, :to_s, {include_blank: false}, {:multiple => false} %>

Upvotes: 2

Related Questions