This gem will be terminated on January 1, 2014! Source code and related gem versions will be completely removed from GitHub and RubyGems. Please use the Rails helpers as you can pass additional attributes (like data-* attributes) to them instead of using this this gem. Example:
@category_options = [ ["News", 1, "data-icon" => "icons/news.png"], ["Software", 2, "data-icon" => "icons/software.png"], ["Books", 3, "data-icon" => "icons/books.png"] ] options_for_select @category_options
Output:
<option value="1" data-icon="/icons/news.png">News</option> <option value="2" data-icon="/icons/software.png">Software</option> <option value="3" data-icon="/icons/books.png">Books</option>
The first and second elements of each sub-array must consist of the option text and value but any number of hash values can be supplied afterward. Here is another example (using additional HTML tag attributes):
@category_options = [["Podcasts", 1, class: "podcasts", title: "A collection of news and discussions"]] options_for_select @category_options
Output:
<option value="1" class="podcasts" title="A collection of news and discussions">News</option>
This format is not limited to the options_for_select helper but can also be used with select and select_tag helpers too. Hence, there is no further need for this gem. Use the Rails form helpers instead!
<img src=“https://badge.fury.io/rb/enhanced_select.png” alt=“Gem Version” /> <img src=“https://secure.travis-ci.org/bkuhlmann/enhanced_select.png” alt=“Travis CI Status” /> <img src=“https://codeclimate.com/badge.png” />
An enhanced select form helper that allows you to use the full HTML spec for select option elements (example: HTML5 data-* attributes, etc.)
-
Fully customizable HTML5 attributes for select options.
Type the following from the command line to install:
gem install enhanced_select
Add the following to your Gemfile:
gem "enhanced_select"
Use the enhanced_select
instead of select
or use enhanced_options_for_select
instead of options_for_select
helper methods in your views. Example:
<%= form_for @post do |form| %> <div><%= form.enhanced_select :category_id, @category_options %></div> <% end %>
…or alternatively:
<%= form_tag "/posts/" do %> <div><%= select_tag "category_id", enhanced_options_for_select(@category_options) %></div> <% end %>
The enhanced_select
or enhanced_options_for_select
method takes all the standard options as the default Rails select method. The only difference is that it expects an array of hashes instead of an array of arrays. For example, in the past you could only define select options as follows:
@category_options = [["News", 1], ["Software", 2], ["Books", 3]]
…but now you can do the following instead:
@category_options = [{text: "News", value: 1}, {text: "Software", value: 2}, {text: "Books", value: 3}]
Notice the use of the :text key in the hashes above. With an enhanced select option, you have to explicitly define what your option text will be instead of the implicit assumption when using the Rails default select options.
In either case, the resulting output would be the following:
<select id="<placeholder>" name="<placeholder>"> <option value="1">News</option> <option value="2">Software</option> <option value="3">Books</option> </select>
Where this gets interesting is when you want to make use of additional option attributes like HTML5 data-* attributes. For example:
@category_options = [ {text: "News", value: 1, "data-icon" => "/icons/news.png"}, {text: "Software", value: 2, "data-icon" => "/icons/software.png"}, {text: "Books", value: 3, "data-icon" => "/icons/books.png"} ]
Which produces the following output:
<select id="<placeholder>" name="<placeholder>"> <option value="1" data-icon="/icons/news.png">News</option> <option value="2" data-icon="/icons/software.png">Software</option> <option value="3" data-icon="/icons/books.png">Books</option> </select>
The possibilities are endless as you can use any valid HTM5 attribute for your select options now.
To test, do the following:
-
cd to the gem root.
-
bundle install
-
bundle exec rspec spec
Read CONTRIBUTING for details.
Developed by Brooke Kuhlmann at Red Alchemist
Copyright © 2010 Red Alchemist. Read the LICENSE for details.
Read the CHANGELOG for details. Built with Gemsmith.