Created
June 25, 2014 10:11
-
-
Save ShPakvel/3ccffcc6138f73b84804 to your computer and use it in GitHub Desktop.
Rails 4 enum and simple_form with I18n
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= simple_form_for(@event) do |f| %> | |
<div class="form-inputs"> | |
<%= f.input :status, collection: Event::STATUSES %> | |
... | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Event < ActiveRecord::Base | |
STATUSES = [ :cancelled, :in_progress, :completed, :invoiced ] | |
enum status: STATUSES | |
... | |
end |
@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. 😄
Another workaround would be Event.statuses.symbolize_keys.keys
. Not pretty but does the job…
@MatthiasGi you are genius, thank you.
👍
@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'
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?
Where is the i18n translation for the enums in that example?
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
You could just use Event.statuses.keys in the form ;)