7

I am trying to make a multi select dropdown work with a dialog for search parameters. I can make the dropdown multi select but can't seem to get/pass the resulting data. (edited/new info will be in italics)

I believe that the root of the problem is that I need to change the permit section in my controller to reflect that I am passing a hash/array. If I look at the resulting record, the 2 fields that I am setting as multi-selects show as nil. However, if I force an errror, the parameters shown by rails show the correct choices. therefore, I believe that the problem might be with the permit section.

That looks like

 *def search_params
      params.require(:search).permit(:document_title,
                                     :summary,
                                     :owner,
                                     :category,
                                     :file_name,
                                     :doc_to_email,
                                     :categories_attributes => [:name])
    end*

I added the :categories_attributes => [:name] to try to get the controller to allow hashes but that didn't work.

The select field is

 <%= f.select :category[], options_for_select(@categories.sort), {:include_blank => true}, {:multiple => true, :size =>10}  %>

but that gives me

.erb where line #41 raised:

wrong number of arguments (0 for 1..2) Trace of template inclusion: app/views/searches/new.html.erb

I thought I had to set category as an array with the [] but obviously I'm missing something.

Category is a string field in the Searches table.

Chris Mendla
  • 987
  • 2
  • 10
  • 25
  • Try like `<%= f.select :category[], options_for_select(@categories.sort), {:include_blank => true, :multiple => true, :size =>10} %>` – Abhi Dec 23 '15 at 12:52

1 Answers1

5

You do not need the [] brackets after the field name as Rails adds those in automatically.

See the example here: http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag

select_tag "colors", "<option>Red</option><option>Green</option><option>Blue</option>".html_safe, multiple: true
# => <select id="colors" multiple="multiple" name="colors[]"><option>Red</option>
#    <option>Green</option><option>Blue</option></select>

In your case the selected values will be available as an array in params[:search][:category] after the form is submitted.

If you use strong parameters, also make sure you have :category => [] in the permit list.

Mate Solymosi
  • 5,699
  • 23
  • 30
  • That didn't work. It seems that when I change the select to multiple, the data does not get written for those fields. I found something that said that I need to change the permitted part of the controller to reflect the fact that I am passing an array/hash. I'm going to change my original post to reflect this info. – Chris Mendla Dec 23 '15 at 17:30
  • THAT WORKED!!! - The link you provided had the solution. I added `:category => []` to the permit section. Parameters that are arrays/hashes are now being passed. – Chris Mendla Dec 23 '15 at 17:39
  • One thing to add, I found that I had to put the array/hash parameter at the end of the permit section.. ie. after all of the other permitted fields. :category => [], :owner => []) – Chris Mendla Dec 23 '15 at 18:24