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).
Related
I'm trying to have an if else statement in the controller to do something if the check_box_tag is checked and something else if it isn't checked.
<%= check_box_tag "apply[]", 1, false %>
If it is checked and the form is submitted "apply"=>["1"] is passed as a parameter, if it isn't checked nothing is sent.
Can I have a "apply"=>["0"] passed as a param if it isn't checked?
The check_box_tag indeed does not use the hidden field so it does not send anything in case the box is unchecked (unlike the check_box helper, which is nevertheless still not useful in case of an array param, as Meier notes above).
If you are trying to use your checkbox in a similar way as in this SO question (i.e. to send a user-selected array of ids for example), the simple solution might be to react on an empty param in the controller. Something in the way of:
params[:apply] ||= []
If, on the other hand, you wanted to use the checkboxes as an ordered array where each value would be determined by its constant position in the array, either true or false, (i.e. if you wanted to get something like this in the apply param: [0, 1, 0, 0, 1]), this wouldn't work. You'd have to choose a different approach, e.g. use the checkboxes with a hash param instead of array.
The rails guide has a warning box about this special case:
http://guides.rubyonrails.org/form_helpers.html
Array parameters do not play well with the check_box helper. According
to the HTML specification unchecked checkboxes submit no value.
However it is often convenient for a checkbox to always submit a
value. The check_box helper fakes this by creating an auxiliary hidden
input with the same name. If the checkbox is unchecked only the hidden
input is submitted and if it is checked then both are submitted but
the value submitted by the checkbox takes precedence. When working
with array parameters this duplicate submission will confuse Rails
since duplicate input names are how it decides when to start a new
array element. It is preferable to either use check_box_tag or to use
hashes instead of arrays.
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]
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!
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)
I want to set a cookie to expire in 1 year if the 'remember me' checkbox is checked.
I have a checkbox form input like:
<%= check_box_tag 'remember', '', false, :class => 'checkbox' %>
What will the value be when it gets posted?
Will it be true/false or checked or 1 or? I set the value to '' in the helper.
Checkbox will return either 0 or 1.
Don't compare with false and true, as this may cause you problems, since 0 is true in Ruby.
In the rails API documentation it has this helpful explanation of missing parameters and the hidden field work-around:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box
check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
Returns a checkbox tag tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). This object must be an instance object (#object) and not a local object. It’s intended that method returns an integer and if that integer is above zero, then the checkbox is checked. Additional options on the input tag can be passed as a hash with options. The checked_value defaults to 1 while the default unchecked_value is set to 0 which is convenient for boolean values.
Gotcha
The HTML specification says unchecked check boxes are not successful, and thus web browsers do not send them. Unfortunately this introduces a gotcha: if an Invoice model has a paid flag, and in the form that edits a paid invoice the user unchecks its check box, no paid parameter is sent. So, any mass-assignment idiom like
#invoice.update_attributes(params[:invoice])
wouldn’t update the flag.
To prevent this the helper generates an auxiliary hidden field before the very check box. The hidden field has the same name and its attributes mimic an unchecked check box.
This way, the client either sends only the hidden field (representing the check box is unchecked), or both fields. Since the HTML specification says key/value pairs have to be sent in the same order they appear in the form, and parameters extraction gets the last occurrence of any repeated key in the query string, that works for ordinary forms.
You get in your action the params
params['remember']
If if check you have the params with false like value. If it's not check you have no params send to your action.