Ruby on Rails 4 Select Multiple

Ruby on Rails 4 select multiple

class and multiple are both part of html_options, so they should go together in a single hash.

Change

<%= f.select :permission, [ ["Read Only", "read"], ["IP Voice Telephony", "ip_voice"], ["IP Video Telephony", "ip_video_telephony"], ["Enterprise Gateways", "enterprise_gateways"], ["Consumer ATAs", "consumer_atas"], ["IP PBX", "ip_pbx"], ["Master of All", "all"] ], {prompt: "Select Permission Level"},
{:multiple => true}, class: "input-lg" %>

To

<%= f.select :permission, [ ["Read Only", "read"], ["IP Voice Telephony", "ip_voice"], ["IP Video Telephony", "ip_video_telephony"], ["Enterprise Gateways", "enterprise_gateways"], ["Consumer ATAs", "consumer_atas"], ["IP PBX", "ip_pbx"], ["Master of All", "all"] ], {prompt: "Select Permission Level"},
{:multiple => true, class: "input-lg"} %>

Right now you are passing them separately. So, the argument count for select method is becoming 5 when it should be 4. Hence, the error.

Saving multiple records with f.select in Rails 4

Either serialize your language column or (the preffered way) create many to many association:

# professor.rb
class Professor < ActiveRecord::Base
has_many :professor_languages, dependent: :destroy
has_many :languages, through: :professor_languages
end

# form:
<%= f.select(:language_ids, options_for_select(Language.all.map {|language| [language.titleize, language.id]}), {}, { multiple: true }) %>

Drop language column from Professor and create ProfessorLanguage, Language models with necessary migrations.

f select multiple select is not working using in form

If you check the implementation of the form builder select method (github), you'll see that the method signature is:

select(method, choices = nil, options = {}, html_options = {}, &block)

multiple flag should be passed using html_options hash, not options. In your case, it should be:

f.select :jobs, options_from_collection_for_select(Demojob.all, 'name', 'name'), {}, :multiple => true

Or, even better, if you prefer new hash syntax:

f.select :jobs, options_from_collection_for_select(Demojob.all, 'name', 'name'), {}, multiple: true

Lastly, there's no need to use options_from_collection_for_select with form builder, you can simply pass the options as arrays:

f.select :jobs, Demojob.all.collect { |job| [job.name, job.name] }, {}, multiple: true

Cheers!

Rails 4: select multiple attributes from a model instance

You will use the method slice.

Slice a hash to include only the given keys. Returns a hash containing the given keys.

Your code will be.

Resource.first.attributes.slice("foo", "bar", "baz")
# with .where
Resource.where(foo: 1).select("foo, bar, baz").map(&:attributes)

Select multiple using Rails `#select` form helper

The signature of the select helper is:

select(method, choices = nil, options = {}, html_options = {}, &block)

multiple is an HTML option, therefore you should use:

<%= tag_fields.select :tags, Tag.all, {}, multiple: true %>

Rails Form: f.select for multiple options

Take a look at the accepted answer to that question - the method signature is:

select(:type, [data], {options hash}, {second options hash})

And in the answer, it has multiple: true in the second options hash.

API dock for select_tag gives a hint about what the two different hashes are for - it looks like the first options hash is for "option_tags", and the second one is for "options"

multiple select with chosen

Solved. The problem was due to turbolinks, the js was not reloading. I had to use the following:
$( document ).on('turbolinks:load', function() {
chosen ... code
});

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



Related Topics



Leave a reply



Submit