Skip to content

Instantly share code, notes, and snippets.

@ShPakvel
Created June 25, 2014 10:11
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShPakvel/3ccffcc6138f73b84804 to your computer and use it in GitHub Desktop.
Save ShPakvel/3ccffcc6138f73b84804 to your computer and use it in GitHub Desktop.
Rails 4 enum and simple_form with I18n
<%= simple_form_for(@event) do |f| %>
<div class="form-inputs">
<%= f.input :status, collection: Event::STATUSES %>
...
<% end %>
class Event < ActiveRecord::Base
STATUSES = [ :cancelled, :in_progress, :completed, :invoiced ]
enum status: STATUSES
...
end
@gabskoro
Copy link

You could just use Event.statuses.keys in the form ;)

@ShPakvel
Copy link
Author

@gabskoro It doesn't work cause simple_form I18n needs symbols and ActiveRecord Enum's keys are strings. Otherwise I would not create separate constant STATUSES. 😄

@MatthiasGi
Copy link

Another workaround would be Event.statuses.symbolize_keys.keys. Not pretty but does the job…

@mygittyhubby
Copy link

@MatthiasGi you are genius, thank you.

@emerleite
Copy link

👍

@faouzzz
Copy link

faouzzz commented Jun 19, 2016

@ShPakvel can you show me where you put translation ?

found it:

it:
  simple_form:
    labels:
      project:
        project_name: 'Nome progetto'
        tasks:
          task_name:  'Nome compito'

@abinoam
Copy link

abinoam commented Aug 6, 2016

@MatthiasGi

Another workaround would be Event.statuses.symbolize_keys.keys. Not pretty but does the job…

But this will (re)create objects at every turn.
I think the trick using a constant fits better, doesn't it?

@aalvrz
Copy link

aalvrz commented Aug 23, 2017

@faouzzz

Where is the i18n translation for the enums in that example?

@anton-osennikov
Copy link

anton-osennikov commented Nov 8, 2018

simple_form i18n translation for the enum in Event.rb example above is found at:

en:
  simple_form:
    options:
      event:
        status:
          cancelled: ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment