rails options_from_collection_for_select the current selection value - ruby-on-rails

I have a select table that is generated using
view :
<select id = "car_type", name="car_type">
<%= options_from_collection_for_select #cartype, 'id', 'name' %>
<select>
and in controller I have this
#cartype = Cartype.find(:all)
I need to get the current selection value, so I can generate a second select table based on the first option selection. I am aware that this can be done with observe_field, however, there is some problems with observe_field, so first I decided to use parameter passing.
Would appreciate for any hints
can anyone point out, how can I get the current selection value and pass the value?

As you can see in the Rails documentation for options_from_collection_for_select the last parameter is the selected value and should be an interger. E.g.
options_from_collection_for_select(#people, 'id', 'name', 1)

Related

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");
})

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).

two select menus with the same parameters in a form 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]

How to display selected value from the drop down menu in Rails?

I am a Rails newbie. I have created a drop down using collection_select and grouped_collection_select. My issue is that I am not able to display the selected option value from the drop down list. I have to display only the selected value on a new page but I am not able to get it.
Use the :selected => params[:name_of_form][:name_of_field]
= f.select :category,
options_for_select(['option', 'value']... etc ),
:selected => params[:user][:category]
I cant remember if the symbol goes on the inside like above, or if it
You should really be more clear when you are asking questions on SO, and you should also include code.
ALWAYS ALWAYS ALWAYS INCLUDE CODE!

radio_button_tag in rails 3.1

I have a query to ask. Im using radio_button_tag with rails 3.1. The issue that I come accross is that for eg : If I have ascending and descending radio buttons, only one should be selected. But in my case both the radio button tags are getting selected.
Kindly help me.
This is a guess at best, because you haven't put your code, but:
You need to ensure that your radio buttons have the same HTML name attribute by giving your tags the same first parameter in radio_button_tag. See the documentation.
if two radio fields has same name, one can be selected at a time!
Pass true to the tag you want to be selected initially. For example, if you want 'Descending' to be checked initially, write like:
radio_button_tag 'order', 'asc'
radio_button_tag 'order', 'desc', true

Resources