two select menus with the same parameters in a form rails - ruby-on-rails

I have a nested form where there are two possible select menus for the same value. At the moment only one of the location_ids is present in the params if both select menus are present.
So even if I select a value in #common_locations_array and leave #possible_locations blank, the parameters do not contain the relevant id. However if I select something from #possible_locations and leave #common_locations_array blank the value is in the params. I guess its to do with the ordering.
=f.simple_fields_for #job.job_locations.build do |p|
=p.input :location_id, collection: #common_locations_array, label: 'Popular Locations'
=p.input :location_id, collection: #possible_locations, label: 'Clients Locations'
So what would be a way around this? Would I specify my own custom parameter for each one of select menus? if so how would you do that?
Or should I merge the two arrays with some kind of divider in the select menu two seperate the two categories? How would I do that?
I'm not sure.
Thanks

It has to do with the name attribute of the input tag that is getting generated. As you have specified the same name to both your input fields, the name that might be getting generated would be same, i.e., job_locations[location_id].
Now, as HTML is executed top-to-bottom, the ones written below will eventually get converted to your prams hash. So, your params[:job_locations][:location_id] will always have the value that is selected in the second drop-down. To avoid this, you can give different names to the two drop-downs.
like
=p.input :popular_location_id, collection: #common_locations_array, label: 'Popular Locations'
=p.input :client_location_id, collection: #possible_locations, label: 'Clients Locations'
Now, both the params can be accessed via: params[:job_location][:popular_location_id] and params[:job_location][:client_location_id]

Related

Rails Form: f.select for multiple options

For my user form, I have a hobbies drop-down menu and I want to be able to select more than one option (one user may have skiing, reading, and chess as hobbies).
Of course, doing this is very easy!
However, none of the available options seem to work for me...
Here's my code:
<%= f.select :hobbies, [['Chess','chess'],
['Movies','movies'],
['Videogames','videogames'],
['Skiing', 'skiing'],
['Reading','reading']],
{:multiple => true} %>
However, when I look at my form, I don't think this is working. It makes the drop-down menu....but how do I select multiple entries? I try ctr + click but it doesn't do anything....what am I missing? It keeps just selecting one value only...
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"

Rails dropdown menu to select object for text_field

I am building a rails form and have an interesting problem I am trying to solve. I can't seem to find anything online to point me in the right direction. Thank you.
Is it possible to use a dropdown menu to select the :object_name for a text field?
In my head, I am picturing a collection_select form helper nested within a text_field form helper, though not sure this is possible.
In the form, I'd like the user to select the proper :object_name from an array
[:object_1, :object_2, :object_3, :object_4]
then give that entry a value with the text field
text_field(object_name, method, options = {})
The objects are all db columns in the same model.
Yes you can do that using jquery.
On change of the object names dropdown value, change the name attribute of the text field.
$('#selectObjectName').change(function(){
var field = document.getElementById("id-of-the-text-field-to-be-changed");
field.setAttribute("name", "value-came-from-the-selected-dropdown");
})

Submit all values in a multiple select box, even when they're not selected (Rails)

I have a multiple select box in a form
<%= f.select :receipts, [], {}, multiple: true -%>
I want to ALWAYS post all options that are in the select box, but HTML only posts the ones the user has clicked on.
I could handle the submit event in JavaScript and set all the options to be selected, but I don't really want the user to see the items get selected. The form may fail to post and I'd prefer not to change anything visual on the screen.
The other thought I had was to use a hidden element and set its value to be a comma separated list of items from the multi-select box just before the submit happens. Then I don't have to mess with the selection state of the multi-select box.
Is there some Rails magic I can use instead of doing this?

Allowing some enabled and disabled option on collection_select

I am trying to populate a dropdown box on a view that has all the states. This works just fine:
<%= f.collection_select :state_id, #states, :id, :name %>
Now, I need to make the following: Some states are going to be disabled for choosing, but they still have to appear on the dropbown list.
How can I achieve this? (I can populate an additional list for these states).
collection_select internally relies on options_from_collection_for_select helper.
Rather than using the collection_select directly, you can use select and pass the result of a options_from_collection_for_select call. The reason you may want to call options_from_collection_for_select directly, is because this method also accepts an optional selected parameter that could be used to pass a value for the disabled items.
selected can also be a hash, specifying both :selected and/or :disabled values as required.
The value of the option can be one of the following
If selected is specified as a value or array of values, the element(s) returning a match on value_method will be selected option tag(s).
If selected is specified as a Proc, those members of the collection that return true for the anonymous function are the selected values.
Therefore, if you pass { disabled: [1, 3, 5] } the items 1, 3, 5 will be disabled. Of course, the value must match the value of the option. You can also pass a block.
To be honest, this Rails helper looks quite complicated to me. Another option is to still use select directly, but create your own helper to generate the string of HTML option items and pass the string directly to the select (which is what options_from_collection_for_select is doing, with a not extremely simple API).

Rails Populating SELECT options from database

I feel like this is basic but can't find details anywhere.
I have a basic application created by generating scaffold.
There is form already built into this that enters data. I have a SELECT (drop down) box in the form. I would like to be able to have the OPTIONS in this select box be pulled from a database. Ideally, the end user would be able to add and edit these options.
I have no idea how to link that SELECT field in my form to where the OPTIONS will be STORED.
Can someone point me in the right direction as far as terminology as what to research? I'm coming up blank. I feel like this must be a common thing to do..
As ByScripts' link includes, here is the script that worked for me (for those where time is not a luxury):
<%= collection_select(:lang_id, 0, Language.all, :id, :name) %>
Where Language is a table with one column called 'name' and an auto-assigned column id; :lang_id is the name of the element and 0 is the default selected index when the page loads.

Resources