Get the selected option's text field in controller in Ruby - ruby-on-rails

I know how to get the value of the selected option in the dropdown in controller. Using
params[:category_id]
My question is for a select like this
<select name="category_id">
<option value="0">New</option>
<option value="1">SubCategory1</option>
<option value="2">SubCategory2</option>
<option value="3">SubCategory3</option>
</select>
How can i also get the selected text after request is posted to controller? So if user selects Option 1, I want "0" as well as "New".

#shaurya, Not sure why you want the text value from the form post. Can you look up the value in config['Categories'] using category_id?

Related

Dojo Select - first option getting selected by default

I have a select box on my form which is getting displayed using addOption. Below the code for the same
storeData = response.myData;
var select = dijit.byId('mySelect');
select.addOption(storeData.items);
HTML declaration of Select box
<select id="mySelect" data-dojo-type="dijit/form/Select" data-dojo-attach-point="mySelect" data-dojo-props="name:'mySelect', placeHolder: 'Select a value', value:''"></select>
In the storeData, I am returning list of JSON objects including a blank option. (label="" and value=" ");
I want the place holder text to get displayed when no option is selected. But by default first option (i.e. blank value option) is getting selected and because of that place holder text is not getting displayed. If I remove the blank value option from list of options, then the option after that is getting selected by default.
Is there any way where I can set the selection to none. Any pointer for this issue?
dijit/form/Select acts as a normal selection box element (simply said it's an enhanced form of <select>). It's the default behavior of such a select box to select the first option by default.
Either way, the dijit/form/Select widget does not support placeholders I think, neither is it documented in the API docs.
As an alternative you could use the dijit/form/ComboBox or dijit/form/FilteringSelect widget, which both support the placeHolder property.
Another common practice (at least with <select>) is that you add a default selected and disabled item, which acts as your placeholder, for example:
<select name="select1" data-dojo-type="dijit/form/Select">
<option disabled="disabled" selected="selected" value="">- Select a state -</option>
<option value="TN">Tennessee</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="FL">Florida</option>
<option value="CA">California</option>
</select>
Demo: JSFiddle

angular & ui-select2: showing preselected value doesn't work

When using the ui-select2 (https://github.com/angular-ui/ui-select2), the preselected option is not shown properly.
I created a plunkr: http://plnkr.co/edit/Ek86jUciPo7rgBnbKdFc
When the page is loaded, the model of the select is set to the second option. And somehow, it is properly set in the select box, see: https://dl.dropboxusercontent.com/u/1004639/stackoverflow/screenshot-select2.png. But the value is not shown above the text box. Or in the select box when the select box is closed.
PS: I tried it without ng-options. Same problem.
I can get it working using ng-repeat and ng-selected. Unfortunately, though, when you use ng-repeat, you can only bind to a string. It's not ideal, but the choice does start out pre-selected.
Here's a working http://plnkr.co/edit/jodn35fvUQpdD2d5BpoC
<select ui-select2="" ng-model="selectedId" >
<option value="">Choose...</option>
<option ng-repeat="option in options" value="{{option.id}}" ng-selected="{{option.id == selectedId}}">{{option.name}}</option>
</select>
And I updated the JS to add this line:
$scope.selectedId = $scope.selected.id;
https://github.com/angular-ui/ui-select2#working-with-dynamic-options
ui-select2 is incompatible with <select ng-options>. For the best results use <option ng-repeat> instead.

How to convert multi select form field with GET tto have propper URL?

<select name="channel[]" multiple="multiple">
<option value="pants">Pants</option>
<option value="tshirts">T-Shirts</option>
<option value="sweats">Sweats</option>
</select>
So, form is GET, and if I select multiple, and when I click on submit URL is like:
?channel[]=pants&channel[]=tshirts
I need it to be like:
?channel=pants+tshirts
This cannot be achieved with an HTML only form. You'll have to incorporate javascript to manipulate the value of channel.

Read only drop down

Is there a good way to make an HTML dropdown read only. Using disabled attribute of select seems to work, but the value is not posted.
<select disabled="disabled">
I have a complex page with lots of javascript and ajax. Some actions in the form cause to drop down to be read only some actions let user decide the value.
Edit: Is there a better way other than using a hidden input?
If you do not want user to pick an option, how is this different from read-only input type="text"?
<select name="selectbox" disabled="disabled">
<option>option 1</option>
<option selected="selected">option 2</option>
<option>option 3</option>
</select>
Correct me if I am wrong, but if an option is selected, the value is sent

Capturing Rails 3 Multiselect Input Params

I have a multiselect form control (fig. 1). When I select more than 1 value, Firefox sends both values (fig. 2). But only the last value gets sent as a input value to my controller (fig. 3). How do I get all those values passed on to my controller?
<form action="html_items/search" method="post" >
<!-- Criteria -->
<div style="float:none;">
<label>Content Provider </label>
<select multiple id="content_provider" name="content_provider">
<option value="FLR">Flare</option>
<option value="SLDT">Slashdot</option>
</select>
</div>
<input type="submit" value="Search" />
</form>
fig. 1
Parametersapplication/x-www-form-urlencoded
content_provider FLR
content_provider SLDT
fig. 2
PARAMs[{"content_provider"=>"SLDT", "controller"=>"html_items", "action"=>"search"}]
...
fig. 3
Thanks
Tim
The first thing is that if you want to create an array of the elements that are selected you need to name your <select> something like <select name='foo[]'>
Next... If nothing is selected nothing will show up. This makes sense, but it stumped me for a bit. I was doing ajax where the left select was search results and the right was the completed list.
I just added a .click(function(){}); to my submit button to set every item on the left to selected with jQuery and the items showed up in the parameters as an array. In the below example you should see the first item show up.
<select multiple id="content_provider" name="content_provider">
<option value="FLR" selected>Flare</option>
<option value="SLDT">Slashdot</option>
</select>

Resources