Search code examples
ruby-on-railsselectinputrubygemssti

Is there a rails gem to use a single table for all of the "options" in multiple select inputs throughout an app?


So, sometimes you just need a list of options for your selects. Is there a simple gem out there that makes it easy to use one table for all the types of options you might have in your app?

This table would likely look like:

id  | type   | value  | label
01  | color  | red    | Red
02  | color  | black  | Black
03  | shape  | circle | Circle
04  | shape  | square | Square
05  | state  | texas  | Texas

For instance, a list of countries, a list of states, a list of colors, a list of months, etc...

Then when using a select:

select_tag :color, options_for_colors

Then it would populate the select with options with values/labels from some options table, where the rows have a type of :color.

This would be easy enough to roll on my own but I don't want to spend the time if its already built.

update

I'd like this to be a dynamic table, so the end user can add/remove items from the select options table


Solution

  • I always use this method,

    app/models/user.rb

    ROLES = %w[admin author normal]
    

    app/views/users/_form.html.erb

    <%= f.collection_select :role, User::ROLES, :to_s, :titleize %>