Ruby on rails drop down - html

I have a simple html select drop down like this
<select name="day">
<option value="1" selected="selected">Monday</value>
<option value="2">Tuesday</value>
</select>
How do i read the value of the selected option in my controller class in ruby on rails.

you can access all form fields with params[:field_identifier]

Related

Which input type is this?

I don't know which type of input this is, What do I have to put in the type=""?
Drop down menus can be created in HTML using the select and option tags. The format looks something along the lines of this:
<select name="foo">
<option value="int">INT</option>
<option value="varchar">VARCHAR</option>
...
</select>
What you are seeing there is a select input with optgroup being used.
<label for="dino-select">Choose a dinosaur:</label>
<select id="dino-select">
<option>Tyrannosaurus</option>
<option>Diplodocus</option>
<optgroup label="Theropods">
<option>Tyrannosaurus</option>
<option>Velociraptor</option>
<option>Deinonychus</option>
</optgroup>
<optgroup label="Sauropods">
<option>Diplodocus</option>
<option>Saltasaurus</option>
<option>Apatosaurus</option>
</optgroup>
</select>
Check out the MDN web docs for it.

Grails g:select add HTML 5 data attributes

I want to load extra data into each select option of Grails g:select taglib. Required output is like the following:
<select id="select">
<option value="1" data-foo="dogs">this</option>
<option value="2" data-foo="cats">that</option>
<option value="3" data-foo="gerbils">other</option>
</select>
I am not able to find a way to add the extra data to the taglib using the data attributes of HTML 5. So how to achieve the similar output?
You can do this by (mis-)using a closure to render the value (called optionKey in Grails select taglib) of the select options:
<g:select from="${books}"
optionKey="${{ book -> "${book.id}\" data-author=\"${book.author.name}"}}"
optionValue="title"
name="selectedBook"/>
This will render the options with a data-author attribute:
<option value="1" data-author="Johann Wolfgang von Goethe">Faust</option>
This works at least in Grails 2.4.4 and Grails 3.1.5.
The grails <g:select /> tag now has the dataAttrs attribute that takes a map which is converted to data attributes as follows:
<g:select from="${dogs}" dataAttrs="[foo: it.species]" name="dog" />
if each dog has an attribute called species whose values are as presented in the question, this code would yield...
<select id="select">
<option value="1" data-foo="dogs">this</option>
<option value="2" data-foo="cats">that</option>
<option value="3" data-foo="gerbils">other</option>
</select>
As mentioned by the comments above this is currently not possible. You need to write your own tag library to achieve this goal.
You can achieve similar result using the following
<select id="select">
<g:each in="${books}" var="book">
<option value="${book.id}" data-foo="${book.slug}">${book.name}</option>
</g:each>
</select>

Make the default option of select tag to be of current value of name in Rails

This piece of code is in my edit user attributes page. I have a subscription_type:string attribute for my model User in my rails app. The default value of the attribute is "". I want the select tag to show the option to be of the current value of subscription_type ("", Silver, Gold or Platinum) for the particular user. How can I achieve this?
<select class="form-group" name = "user[subscription_type]">
<option value="">Select your subscription type</option>
<option value="Silver">Silver</option>
<option value="Gold">Gold</option>
<option value="Platinum">Platinum</option>
</select>
You're going to want to use the select_tag and the options_for_select methods and pass it an array of arrays- something like
options_for_select([['Gold', 'Gold'], ['Silver', 'Silver'],['Platinum', 'Platinum']], #user.subscription_type)

Dynamically change value of a <select> inside cell of a table

i'm developing an asp.net/vb.net webapp.
I've an html table with some columns.
I get the values of the cells from a database, so i've a dinamically number of rows.
In columns i need to put a tag, because i want to allow the user to change the value of that cell.
Of course the initial value of the select should have to come from the query, but i dont know how to do this.
Something like
...
<td> <select selected="<%= queryresult("id").value %>">
<option value="1"> option1 </value>
<option value="2"> option2 </value>
</select></td>
...
but obv not working.
I just hope you understand what i want.
Thanks.
You can find the solution for your problem posted here but implemented with PHP. Anyway you can do it with ASP.NET as well. Basically you have to check if the current option value has the value from the query and add selected="selected" to the specific option tag. Also the correct markup for a select is like this:
<select>
<option value="1" selected="selected">Option 1</option>
<option value="2">Option 2</option>
</select>

Several simple select boxes to replace a multiple select box in HTML

I'd like to replace a multiple select box like:
<select multiple="multiple" name="options">
<option value="option1">option1</option>
<option value="option2">option2</option>
...
</select>
with an arbitrary number of simple select boxes:
<select name="options1">
<option value="option1">option1</option>
<option value="option2">option2</option>
...
</select>
<select name="options2">
<option value="option1">option1</option>
<option value="option2">option2</option>
...
</select>
Is there any way to send and retrieve via POST an array of select boxes or should I try to access every select box named options(number) until it fails? Seems a bit dirty.
I should be able to submit an action to "delete this select box" or "create new select box" so I need some way to distinguish the select boxes.
Just give the select elements the same name.
HTML forms have no concept of "an array". Every form handling library that handles arrays of input data generates them from a name having multiple values:
foo=bar&foo=baz&aDifferentField=fizzbuzz
This is what a multiple select (named foo) with two values selected will generate (when there is 'aDifferentField' in the form too).
Sometimes there are provisos involved.
Perl's CGI.pm needs the request for the data to be in list context:
my #foos = $cgi->param('foo');
PHP requires the name to end with the characters '[]'
name="foo[]"
foo[]=bar&foo[]=baz&aDifferentField=fizzbuzz
… but it all comes down to the names being the same (although the ids must still be different).
As for the deletion:
<label for="foo5">Group 5</label>
<select name="foo" id="foo5">
<option value="delete_foo5">Delete this group</option>
<option value="1">1</option>
<option value="2">2</option>
</select>