state_machine状态的翻译

时间:2012-05-18 14:16:03

标签: ruby-on-rails internationalization

我在ActiveRecord中使用pluginaweek's state_machine进行Tester-Management。我通过这个调用检索所有可能状态的数组:

irb(main):013:0> Tester.state_machine.states.map &:name
=> [:candidate, :contract_sent, :contract_received, :contract_wont_return, :active, :retired]

现在,我不想使用meta_search构建搜索表单来检索具有特定状态的测试人员。我想使用一个选择:

- states = {}; Tester.state_machine.states.map(&:name).each{ |e| states[t(e.to_s)] = e }
= form.input :state_equals, :as => :select, :collection => states

它有效,但它并不好,因为我构建了一个新的哈希并翻译每个元素。此外,翻译必须插入两次,因为我的解决方案不使用I18n yaml-structure for state_machine ...

是否有类似

的方法
Tester.state_machine.states.map(&:human_state_name)

Tester.state_machine.human_states_name.map(&:name)

获取翻译状态?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。您可以使用以下命令获取state_mache状态的翻译:

- states = {}; Tester.state_machine.states.map() { |s| states[s.human_name] = s.name }
= form.input :state_equals, :as => :select, :collection => states