简体   繁体   中英

Allow rails-bootstrap-form collection to accept enumerize values

I am trying to drop in enum values setup with the 'Enumerize' gem into the radio collection select field on a rails-bootstrap-form .

An example is shown how to do this with an ActiveRecord collection. How should I modify the example

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

So that it can accept an enum that is setup on my model

<%= f.collection_radio_buttons :level %>

skill.rb

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

The Enumerize documentation says

SimpleForm

If you are using SimpleForm gem you don't need to specify input type (:select by default) and collection:

 <%= simple_form_for @user do |f| %> <%= f.input :sex %> <% end %> 

and if you want it as radio buttons:

 <%= simple_form_for @user do |f| %> <%= f.input :sex, :as => :radio_buttons %> <% end %> 

If you're using simple-form, then the documentation says all you need to do is this:

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

  <%= f.input :level %>

<% end %>

If you're using rails-bootstrap-form, the documentation shows how to use collection_radio_buttons with Rails's built-in enum functionality, which you aren't using. So I believe you would need to pass in the Skill.level.options call as the collection values, but the :level method for (as the method , value_method , and text_method arguments):

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

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

<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM