Browse the Ruby on Rails Community.

You are here: Browse Railsplugins Multiple Select

Multiple Select

= Multiple Select Helper

IMPORTANT: This version is for Rails versions 1.2 or greater. If you want to use the plugin with Rails 1.1 download a version of the plugin for your version of Rails. More information at http://ruido-blanco.net/blog/rails-multiple-select-helper-plugin

IMPORTANT: If you used the plugin prior version 20070407 and you don’t want to change your code you should stick to use the last stable version 20060918 (you can found it at http://svn.ruido-blanco.net/multiple_select/tags/multiple_select-20060918). The easiest way to port your code to the new version is change all call to multiple_select, collection_multiple_select and tree_multiple_select to its *_tag versions, but maybe you want to work a bit more and use the new “object-method” parameters versions, they simplify the controllers a lot.

Selecting multiple elements in a list is sometimes tricky. You may click inadvertably on one item of the list and lost all your previous selection. You are forced to use Ctrl (or Command) clicks to select more than one element.

Multiple Select Helper allows you to create easy to use list for multiple selections from array, hashes, collections or trees. The list is build using checkboxes so you can click easily and you will not lost the elements you clicked before. As drawback you lose the use of the keyboard in the “list”.

This multiple selections are very useful in many to many relationships (has_and_belongs_to_many or has_many :through with no obligatory fields) where a “add and remove” solution will be cumbersome.

You can download this plugin at:

http://svn.ruido-blanco.net/multiple_select/trunk

You can find this information at:

http://ruido-blanco.net/blog/rails-multiple-select-helper-plugin

== ‘myclass’

And every call to every method of the module will use an default outer_class of myclass (you can always pass a new value in the options hash and it will be used instead of the default value).

There is an additional variable that you can not set using the options hash of the methods but you can set using a module variable. This variable is call is list_tags and is an array of two strings that will be used to wrap the list and the individual list items, respectively. So if you want your checkboxes wrapped by divs like in pre-20060917 versions you can set the variable like this:

FightTheMelons::Helpers::FormMultipleSelectHelperConfiguration.list_tags =
  ['div', 'div']

Note that this line does NOT produce the exact same results as pre-20060917 versions of the plugin in the tree methods (collection and normal methods produce the same results, as far as I know).

When you want to store your list of checked options in a “habtm” relationship you could use something like:

  1. In the model class Person < ActiveRecord::Base has_and_belongs_to_many :fruits end
  1. In the view <%= collection_multiple_select( ‘person’, ‘fruit_ids’, Fruit.find(:all), :id, :name ) %>
  1. In the controller @person.fruit_ids = params:person

And you will have all the fruits you have selected linked to the person you are editing.

Version history

- 20070409 - The old hidden_field toggled by include_hidden_field is now always included (seems like is really needed in Rails 1.2). - In some situations with the object-method versions of the helpers Rails will fail saying that content_tag expects 3 parameters instead of 2. - 20070407 - Merge with the "Object Method Parameters" branch. - Old versions of the methods are still accessible in the "_tag" versions. - include_hidden_field removed (not needed in Rails 1.2). - 20060918 - Changed the hidden_field option to include_hidden_field so it do not clash with Rails hidden_field method. - Moved all the configuration to a new module not included in ActionView. - 20060917 - Added hidden_field option. - Change the default tags to and . - Added module-wide variables for most used options. - 20060903 - Added disabled option. - The ids generated are now HTML 4.01 valid. - The tree version is more efficient now. - 20060806 - Initial release.

Authors

- Daniel Rodríguez Troitiño (mailto:drodrigueztroitino@yahoo.es): main programmer. - Michael Schuerig (mailto:michael@schuering.de): some ideas.

NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly

Users


See all 10 member details


Membership

+ Join this railsplugin

Record Maintainer

'None'