Ruby on Rails 4 - Simple_Form Multiple Select Input

Ruby on Rails 4 - simple_form multiple select input

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 => []).

Simple form multiple select

I have solved a first party of my problem,

I have an available beautiful number check:

<%= simple_form_for @sudoku do |f| %>
<ul>
<% (1..9).each do |x| %>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="checkbox" name="sudoku[number]" id="optionsCheckboxs<%= x %>" value="<%= x %>" autocomplete="off" >
<%= x %>
</label>
</div>
<% end %>

but i don't solved my problem for the multiple selection..

If I add:

name="sudoku[number][]"

I have a this rollback problem:

Processing by SudokusController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Cw/......", "sudoku"=>{"number"=>["1"]}, "commit"=>"Submit"}
Unpermitted parameters: number

If you have the solution....I am ok!!!!

Thanks

Making multiselect work with simpleform, in rails

Build a joining table with a many-to-many relationship. This separates the problem, allows adding the extra fields without too much work.

Create migration table/model X, and reference the desired fields, in this case use subjects, curriculum, grades and teacher_profiles.

Create a many-to-many relationship in this table, then make the new model nested under teacher_profiles.

The video "Rails - Nested Forms in Rails | [Tutorial]" is useful.

How can I have a multiple grouped select with simple_form?

I figured it out on my own.

This produced the correct output:

<%= f.association :tags, as: :grouped_select, collection: TagGroup.all, group_method: :tags, input_html: { :multiple => true } %>

The key part being:

input_html: { :multiple => true }

Multiple select in collection

app/models/your_model.rb:

class YourModel < ApplicationRecord
serialize :activity, Array # add this

# you can't use `inclusion:` validation because it is only for validating a single value
# so you'll do something like this, or if you want it to clean it a bit by defining a method instead, see here https://stackoverflow.com/questions/13579357/validates-array-elements-inclusion-using-inclusion-in-array
validate do
unless %w(Vente Location Syndic Gestion).include? activity
errors.add(:activity, "#{activity} n'est pas un mandat valide"
end
end
end

app/views/.../some_view.html.erb:

<%= f.input :activity,
required: true,
autofocus: true,
label: "Votre Activité",
collection: ["Vente", "Location", "Gestion", "Syndic"],
input_html: { multiple: true },
include_hidden: false
%>
  • I added input_html: { multiple: true } above so that the params will be an array of values instead of just a single value

  • I added that include_hidden: false above because without it you'll get something like

    ["", "Location", "Syndic"]

    which adds an empty string instead of just..

    ["Location", "Syndic"]
    • more info here

app/controllers/some_controller.rb:

devise_parameter_sanitizer.permit(
:account_update,
keys: [
:password,
:password_confirmation,
:photo,
:address,
:cover,
activity: [] # change this from :activity
)

Tested working

Simple form multiple select with simple array

If you want to save an array in your battery field u can use

serialize :battery, Array

in model

Simple form multiple select with checkboxes

After a couple more days of researching I finally figured out the answer. First I had to add

 serialize :ethnicity, Array

to the top of the search model. Then in the same model, while following Ryan Bates advanced search video, I corrected my query to look like this

users = users.where("ethnicity in (?)", ethnicity) if ethnicity.present?

Thanks Kirti Thorat for all your help!



Related Topics



Leave a reply



Submit