Rails Form: f.select for multiple options - ruby-on-rails

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"

Related

Rails select opton using images

i have a select_tag in my rails form where i want to show images as option, i tried searching for a solution online but there is nothing quite good.
my form
<%= form.select :transport, ['Select an image', 'http://pngimg.com/uploads/tesla_car/tesla_car_PNG24.png', 'http://pluspng.com/img-png/png-hd-bike-ktm-duke-bike-png-download-1600.png' %>
i want something like this:
click here
Instead of struggling to let Rails handle it, It's recommended to use JQuery to populate options in the drop-down list.
This link guides you through the process to do it.
Click Here

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

gmail-like popup checkbox list in rails?

I need to build something like what gmail does for it's labels... It has a button that when pressed pops up a scrolling list displaying the labels with checkboxes for selection.
I'd like to hear about approaches to do the popup and how to place it right under the button.
Also, I'd like to be able to observe the checkbox select/deselect events and take action, so advice on that part would also be appreciated... otherwise, I guess I'll have to put a form with a submit button and handle the new selections when the user submits.
If the checkbox list is static, you can do all this directly in the rendered action. Otherwise, two approaches are possible:
Use button_to_remote to retrieve an action displaying the popup and also serving the necessary js;
Use button_to_function to retrieve some XML or json (at your option) from an action, with the necessary labels and values for checkboxes, then render the popup.
The first may be easier to do if you're not familiar with all this, while the second is way more efficient, as only data is passed through the asynchronous call, and not markup nor javascript.
About your last question, if (un)checking the checkbox must result in a server side action, prototype_helper provides a convenient observe_field function, to be used like this:
<%= check_box "foo", "bar" %>
<%= observe_field "foo_bar", :url => {:action => :some_action, :controller => :some_controller} %>
If the (un)checking can be managed on client side, you can simply use:
<%= check_box "foo", "bar", { :onclick => "someFunctionToDoWhatINeed(someArg);"} %>
Just two notes:
JavascriptHelper and PrototypeHelper are just this, helpers: they allow you to do some things with a very simple syntax and are great, as long as they are helping; when they are no more, feel free to drop them and go for plain javascript.
I've used prototype for a while, but then I fell in love with jquery; you may want to take a look at it.
Please edit your question or comment my answer if I didn't understand your question and/or was unhelpful.

Resources