2015-03-31 23 views
0

我想將枚舉值設置爲'Enumerize' gem放入rails-bootstrap-form上的收音機選擇字段。允許rails-bootstrap-form集合接受枚舉值

一個示例顯示瞭如何使用ActiveRecord集合執行此操作。我應該如何修改這個例子

<%= f.collection_radio_buttons :skill_level, Skill.all, :id, :name %>

因此,它可以接受一個枚舉是建立在我的模型

<%= f.collection_radio_buttons :level %>

skill.rb

class Skill < ActiveRecord::Base 
    extend Enumerize 
    enumerize :level, in: [:good, :ok, :bad ] 
end 

Enumerize documentation

SimpleForm

如果您正在使用SimpleForm寶石你不需要指定輸入類型 (:默認情況下選中),並收集:

<%= simple_form_for @user do |f| %> 

    <%= f.input :sex %> 

<% end %> 

,如果你想把它當作單選按鈕:

<%= simple_form_for @user do |f| %> 

    <%= f.input :sex, :as => :radio_buttons %> 

<% end %> 

回答

0

如果你使用的簡單形式,則文件說,所有你需要做的是這樣的:

<%= simple_form_for @skill do |f| %> 

    <%= f.input :level %> 

<% end %> 

如果您使用rails-bootstrap-form,文檔將顯示如何使用collection_radio_buttons以及您未使用的Rails內置枚舉功能。所以,我相信你會需要在Skill.level.options通話的收藏價值傳遞,但:level方法(爲methodvalue_method,並text_method參數):

<%= bootstrap_form_for @skill do |f| %> 

    <%= f.collection_radio_buttons :level, Skill.level.options, :level, :level %> 

<% end %>