Rails - Pre select multiple options with grouped_options_for_select - ruby-on-rails

I would like to preselect multiple options when using grouped_options_for_select in rails 4.1
The method signature is
grouped_options_for_select(grouped_options, selected_key= = nil, options = {})
The selected_key only allows a single value which matches exact options. I would like to preselect multiple options since i am using a multi-select. Is there anyway to do this?

You can make selected_key an array of values for exactly your case.
From the documentation:
"selected may also be an array of values to be selected when using a
multiple select."
If you look at the source for grouped_options_for_select it just calls options_for_select passing selected_key.

Related

Empty default value for select year

I'm working on a form using ruby on rails and I have a select tag to choose a class year
=select :student, :class_year, (Time.zone.now.year - 30)..(Time.zone.now.year + 10)
This gives me the default value the current year - 30. I tried to add an empty default value but I get errors and I can't figure out how to get it to work. I'm very new to ruby.
I would appreciate any help.
It looks like the problem is that you're using a Range as the third parameter for the select method. As stated in the Rails API documentation, this choices parameter needs to be either a flat collection (see options_for_select) or a nested collection (see grouped_options_for_select).
In your case, rather than (Time.zone.now.year - 30)..(Time.zone.now.year + 10), which results in a Range, you could use the following to produce an array of strings:
((Time.zone.now.year - 30)..(Time.zone.now.year + 10)).to_a.map(&:to_s)
I suggest you take a look at https://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease for some excellent guidance about how to use the select form options helper.
I hope this helps!

Select or input

In my app I have a form for creating new trip. Model Trip has field finish_address. For setup the finish_address in I use select in simple form to select address from user's addressbook. But I want to make a better form: if there isn't necessary address, user can add it using input field.
So I need to make form with to types of setup finish_address. How can I make it?
You have 2 basic options.
You can use autocomplete on a text input, populating a dynamic crop-down with know values.
There are several gems available to get you started in this direction, like https://github.com/crowdint/rails3-jquery-autocomplete.
Alternatively, you can add an "other" option to a select input, populated with your known values. When the "other" item is selected, display a previously hidden text input with the same name below the select input. The "lowest" element will take precedence, for what gets sent to the controller.
In the controller, just do a find_or_create_by, using your provided value.
These options both require javascript, but you can eliminate the need for javascript if you make your select a non-db-backed attribute, and manipulate your params accordingly, as they come in to the model. This might help with validations, as well.

Accessing a member in the hash

I have a JSON that controller generates and in debug window it looks like this:
So basically this hash has two main elements, one is medications and one is query_duration. And medications its self is has a few members.
To draw some charts I need to pass the whole Medications part of this to the Javascript, I used GON gem for passing to JavaScript. But my problem is that I don't know how to pass the medications part?
Should I say like #order_summary[0] #order_summary.medications ? none of them worked tho.
#order_summary['medications']
Should work.

Dynamic filter in spree

I use spree(v. 0.70.5) an I need realize dinamic filters for product. Options for the filter defined by the current taxon.
What should I do?

get a array use params

In rails , I met a question!
when i use the multiple select ,i don't know ,how can i get the all parameter values ,it likes
in javaEE,
String[] strs=request.getParameters("aaa");
so,who can tell me the method in rails to realize the function?
Thank you in advance!
You search for "params". See http://api.rubyonrails.org/classes/ActionController/Base.html#Parameters for reference (subsection #Parameters). You can access the "aaa" parameter (GET/POST) using params[:aaa].
If you have a multi select in your form that value will just be an array of those values.

Resources