0

I'm getting errors when I try to display a select dropdown of enums from my model.

I have defined an enum in my model Plants:

class Plant < ActiveRecord::Base  
  belongs_to :garden

  enum life_cycle: [ :annual, :perennial, :biennial ]
  enum sun: [ :full_sun, :part_shade, :full_shade ]
  enum sow_method: [ :direct, :indoor, :direct_indoor ]
end

I want the corresponding input to display those enum options. I see from Saving enum from select in Rails 4.1 it can be approached like this (in _form.html.haml):

= simple_form_for(@plant) do |f|
  = f.error_notification

  .form-inputs
    = f.input :name
    = f.input :scientific_name
    = f.input :height
    = f.input :width
    = f.input :spacing
    = f.input :life_cycle, :as => :select, :collection => Plant.life_cycle.keys.to_a
    = f.input :sun
    = f.input :sow_method
    = f.input :direct_seed_start
    = f.input :direct_seed_stop
    = f.input :indoor_seed_start
    = f.input :indoor_seed_stop
    = f.input :transplant_start
    = f.input :transplant_stop
    = f.association :garden

  .form-actions
    = f.button :submit

When I try to visit the edit page I get an "undefined method" error. I'm very new to ruby so I'm probably misunderstanding something simple...

Thanks

enter image description here

Community
  • 1
  • 1
doub1ejack
  • 10,627
  • 20
  • 66
  • 125
  • I'm just unclear on the syntax. Do I just need to I stick `= f.select ...` in front of the `= render 'form'` line? In that situation, I don't see what `f` would be referring to. Is there any reason to think that `'form'` would render differently? – doub1ejack Sep 28 '16 at 15:21
  • 1
    `= render 'form'` is nothing but a partial `_form.html.haml` which would be containing the form where you need to put your `= f.input :life_cycle, :as => :select, :collection => Plant.life_cycle.keys.to_a` – dp7 Sep 28 '16 at 15:29
  • I think it needs to be `life_cycles`. I changed it in my answer. A good way to get such an information is to call the method `methods` on your `Plant` class (in the live shell for example). – Max N. Sep 29 '16 at 12:26

2 Answers2

0

I have done something like this in one of my forms (using simple_form):

= f.select :life_cycle, Plant.life_cycles.keys
Max N.
  • 993
  • 1
  • 12
  • 31
0

Few examples before the answer

# Assume life_cycle was set to 'annual'
puts p.life_cycle 
#=>  "annual"
p.life_cycle = 0 # set life_cycle as 0
puts p.life_cycle 
#=> "annual"
p.life_cycle = 2 # we set to biennial
puts p.life_cycle
#=> "biennial"

You could also do this to see if life_cycle is annual by p.annual? results in true or false

And when you do this, using a plural of the enum, you can access it on class as a class method.

puts Plant.life_cycles
{"annual" => 0, "perennial" => 1, "biennial" => 2}

So yeah as @razr describes you would use the class method to get the hash and extract keys to form your select menu which is Plant.life_cycles.keys

Pramod Solanky
  • 1,690
  • 15
  • 17
  • ug, the plurals! not entirely used to that yet. nice to know that's the only thing I had wrong though. thanks – doub1ejack Sep 29 '16 at 20:30