get a array use params - ruby-on-rails

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.

Related

FlutterFlow- How to remove/delete item from datatype Map field?

Having trouble removing items from a datatype (map) field. Do I need to write a custom function for this? How would I remove just one item from this field by using the contactUID and leave the others? I've googled and youtubed and found zero guidance. None of the attempts I've tried using FF functionality seem to work. Any ideas?
you can delete using this function.
eventContacts.removeWhere((element) => element['contactUID'] == 'XXXX');

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!

Rails - Pre select multiple options with grouped_options_for_select

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.

Format rails data

I have such integer values like yearmonth, but i wont to see month-year, how I can do this in rails view?
for example
197903 198812
must be viewed as
03-1979 12-1988
try this
"#{197903.to_s[0...4]}-#{197903.to_s[4...6]}"
There may be a more elegant way of doing this if you can convert it to a date object first.

Individual Form Params when POST data is split

I'm trying to access a date variable from a form individually to the other variables. The form is a formtastic plugin form. The issue I am facing is that the date is a three-part dropdown and simply doing params[:friend][:born_on] doesn't seem to be doing the trick as it returns NULL.
Here is my parameters output;
Parameters: {"commit"=>"Create", "action"=>"create", "authenticity_token"=>"6+PyuqUNQySe29iEF69PIFvv6DOie5bp4jZAcRva85c=", "controller"=>"friends", "friend"=>{"born_on(1i)"=>"1973", "born_on(2i)"=>"3", "born_on(3i)"=>"5", "is_female"=>"false", "last_name"=>"Smith", "first_name"=>"John"}}
I want to use the variable to set another method; event.happening_on
Any help is appreciated- thanks!
The reason params[:friend][:born_on] is not returning anything is because that's not the name of the parameter.
It looks like the values you're looking for is one of: params[:friend]["born_on(1i)"], params[:friend]["born_on(2i)"], or params[:friend]["born_on(3i)"]. Which corresponds to what looks like, year, month and day respectively.

Resources