Ruby on Rails 4 - simple_form multiple select input
Asked Answered
C

3

27

I have a simple_form input field that looks like this:

<%= f.input :particular_users, collection: @all_users, input_html: { class: 'multiselectuser', multiple: true} %>

When I leave multiple: true off, the form submits the chosen value for the parameter :particular_users and I can see the value when debugging using "raise params.inspect". However when I leave the multiple: true option there, no vales get passed for the parameter :particular_users.

What am I doing wrong?

EDIT: I can not use the association input because :particular_users is a virtual attribute and has no relationship. I want the multiple select box to pass whatever values that are in there, even if they are arbitrary.

Chartist answered 3/3, 2014 at 23:17 Comment(0)
C
16

It actually does work the way I wanted it to. The trick is to tell the strong parameters to allow a hash. It doesn't throw a strong parameters error, the param just gets thrown out and doesn't come through. So I set it to for example: params.require(:survey).permit(:particular_users => []).

Chartist answered 4/3, 2014 at 1:23 Comment(1)
Did you figure this out? I have an array whitelisted in my strong params but cannot figure this out. #41628457Wellborn
F
60
f.input :days, collection: @your_collection, input_html: { multiple: true }
Farci answered 1/4, 2015 at 13:19 Comment(3)
can you explain what you are doing here. that would help out a lotHustle
if you use simple_form gem and want multiple select just add this attribute to input_htmlFarci
For some reason, it is not allowing to unselect.Tawnytawnya
C
16

It actually does work the way I wanted it to. The trick is to tell the strong parameters to allow a hash. It doesn't throw a strong parameters error, the param just gets thrown out and doesn't come through. So I set it to for example: params.require(:survey).permit(:particular_users => []).

Chartist answered 4/3, 2014 at 1:23 Comment(1)
Did you figure this out? I have an array whitelisted in my strong params but cannot figure this out. #41628457Wellborn
E
4

To create multiple select tags with simple_form, use:

<%= f.association :particular_users, collection: @all_users, input_html: { class: 'multiselectuser'} %>

see part Associations in the gem description.

But as you don't want to use an ActiveRecord associaion, use select_tag:

<%= select_tag 'particular_users', 
       options_from_collection_for_select(@all_users, :id, :name), 
       multiple: true, class: 'multiselectuser' %>
Embrace answered 3/3, 2014 at 23:27 Comment(3)
I receive the error: Association :particular_users not found because :particular_users is a virtual attribute and not an association. I don't want to select an association, I just want the params to pass what is in the multiple select box even if they are arbitrary values.Chartist
then simple_form is not the right tool for you. Use select_tag to build your custom select.Embrace
It is actually what I want. I posted the correct answer.Chartist

© 2022 - 2024 — McMap. All rights reserved.