views:

85

answers:

1

I have a Rails 2.3 web application that uses the collection_select helper with :multiple => true to handle a habtm relationship. This is working fine to set one or multiple values, however I have not figured out how to allow to REMOVE all selections.

Code:

<%= f.collection_select :category_ids, Category.find(:all), :id, :name, 
    { :selected => @entry.category_ids },
    { :multiple => true, :name => 'entry[category_ids][]' }
%>

Once the user has ever set a category for an entry, how would I go about allowing it to be removed, so that this entry has no category? Is this possible with collection_select or would I need to add a checkbox to handle this specially?

P.S: I already tried with :prompt, :include_blank and :allow_blank, but as far as I could see neither of them did anything.

A: 

In your controller's update action, put in the following line:

params[:entry][:category_ids] ||= []

before the call to Entry.find.

dseifert