Help with rails collection select - ruby-on-rails

I need to add different values for each option tag in my collection_select cause Im trying to use this jquery plugin.... How do I do that?
Heres my collection select code
<%= e.collection_select(:id,State.all,:id,:name) %>
The output should be something like
<select name="state[id]" id="state_id" class="selectable">
<option value="">-- select --</option>
<option value="1" title="florida">Florida</option>
<option value="2" title="georgia">Georgia</option>
</select>
Please help.

Hmm, I believe you gotta write your own helper to do so.

Related

Dynamically populate select dropdown from parameters

How would I populate a select dropdown in my view to be the value of a certain parameter:
http://localhost:3000/?group_size=6 - where I want the dropdown selects to be 1-6 as so:
<select name="group_size">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
<option value="6">Six</option>
</select>
Best would be to display the text for the number, but at least to create the text fields without names would be needed. This is Ruby 2.3 Rails 4.2. What do I need to write in the view and/or controller to make this happen?
In your view you could do something like...
<select name="group_size">
<% (1..params[:group_size]).each_with_index do |i| %>
<option value="<%= i %>"><%= i %></option>
<% end %>
</select>
This creates a Range between 1 and whatever group size is passed. See the Ruby documentation for each_with_index here.
Note: This will break if no group_size is provided. I'll leave it to you to guard against nil. :)

show "select option" as default value in JQuery UI select

I am using the JQuery UI to be able to show images within a select box:
see
http://jqueryui.com/selectmenu/#custom_render
But how using this can I make "Select an option" the default value that appears within the selectbox but also does not show when the select drops down?
Thanks!
HTML code is as follows
<fieldset>
<select name="filesA" id="country">
<option data-class="austria" /> Austria</option>
<option data-class="belgium"/> Belgium</option>
<option data-class="bulgaria" />Bulgaria</option>
...
</select>
</fieldset>
include "Select an option" as one of the option and disable the option, So the user can't select it. Here is the code
<fieldset>
<select name="filesA" id="country">
<option selected="selected" disabled="true" /> Select an Option</option>
<option data-class="austria" /> Austria</option>
<option data-class="belgium"/> Belgium</option>
<option data-class="bulgaria" />Bulgaria</option>
</select>
</fieldset>

Can't generate Chosen select using Rails helpers

I have styled beatiful select, but I need to connect it to database, so I have simple html code:
<select class="chzn-select" tabindex="1" style="width:300px;" data-placeholder="Choose a Category">
<option value=""></option>
<option value="Fashion">Fashion</option>
<option value="Coupon">Coupon</option>
<option value="Sport">Sport</option>
</select>
and code using rails form helpers:
<%= f.select :category, options_for_select(%w[Fashion Health Travel Food Coupons]), :class => "chzn-select"%>
EDIT: Add html code generated by rails :
<select id="website_category" name="website[category]"><option value="Fashion">Fashion</option>
<option value="Health">Health</option>
<option value="Travel">Travel</option>
<option value="Food">Food</option>
<option value="Coupons">Coupons</option></select>
It shows me blank page, however code is generating due to I see it in my Google console.
I should set all other attributes or what is porblem ?
What does you generated code look like? Can you post that?
If you're not generating any options, maybe try:
<%= f.select :category, options_for_select(["Fashion", "Health", "Travel", "Food", "Coupons"]), :class => "chzn-select"%>
I was needed to call it correctly from js file:
$("#website_category").chosen();

grails g:select tag

I am using a g:select tag like this:
<td><g:select name="newCity"
id="${'newCity_' +cityData.uid}"
from="${cityData.name}"
value="${cityData.someValue}"
noSelection="${['null':'Select City...']}" class="newCity" />
</td>
which renders the following HTML:
<td>
<select name="newCity" id="newCity_abc" class="newCity" >
<option value="null">Select City...</option>
<option value="A" >A</option>
<option value="B" >B</option>
<option value="C" >C-</option>
<option value="D" >D</option>
</select>
</td>
However, I want my HTML to look like this; with class inserted since I am doing some javascript validation:
<td>
<select name="newCity" id="newCity_abc" class="newCity" >
<option value="null">Select City...</option>
<option value="A" class="populated" >A</option>
<option value="B" class="notpopulated" >B</option>
<option value="C" class="populated" >C</option>
<option value="D" class="notpopulated" >D</option>
</select>
</td>
Is this possible?
Do I need to create a custom tag library to acheive this?
Any help will be appreciated, Thanks!
I don't believe this is possible. The select tag implementation (which writes out the <option> tags) calls the private method writeValueAndCheckIfSelected to fill in the option, and this is not aware of any class names.
There is a 2 year old New Feature Request on the Grails JIRA, but I think you may be stuck rolling your own tag to do this for your specific situation.

Help with asp.net mvc and Select List Dropdown

I want to build this dropdowns with these specific attributes.. How can I do this? Im using LinqToSql. Each Company has employees
<select name="companies" id="companies" class="selectable">
<option value="">-- select --</option>
<option value="1" title="company1">Company1</option>
<option value="2" title="company2">company2</option>
<option value="3" title="company3">company3</option>
<option value="4" title="company4">company4</option>
</select>
<select name="employees" id="employees" class="selectable">
<option value="">-- select --</option>
<option value="1" class="company1">John</option>
<option value="2" class="company1">michale</option>
<option value="4" class="company2">brian</option>
<option value="5" class="company2">mark</option>
<option value="6" class="company3">daniel</option>
<option value="7" class="company3">rose</option>
</select>
Do I have to build my own helper? please help.
this all comes from Database and I have Company and Employee with LINQTOSQL classes
As far as I know yes... The default html helper doesn't give you the ability to set the title or the class attribute on the options...
I would recommend building your own (or looking around to see if someone else has done one that solves this problem).
If you do build your own I would use lambda's (anonymous linq delegates) to capture/define the attributes your are after for the options. That way you can reuse it over and over. Let me know if you need help with this.
In asp.net MVC you already have a lot of built in Helpers. For the dropdown I would do something like this :
<%= Html.DropDownList("<name>", "<selectlist collection>", new { #class = "<css class>", title = "<title>" }) %>

Resources