I have a select dropdown in a form but was looking to set the prompt value as "0"
<%= f.select :image_size_id, options_for_select(#image_sizes.collect{ |i| [i.dimension] }), { prompt: "Please select a Print Size" }, id: 'image_size_select' %>
This generates
<select id="image_size_select" name="cart_item[image_size_id]">
<option value="">Please select a Print Size</option>
<option value="1">10x8</option>
<option value="2">A4</option>
<option value="3">A3</option>
<option value="4">A2</option>
</select>
Using jQuery I can do this
$(document).ready(function(){
$('#image_size_select option[value=""]').val('0')
});
But I was looking to do this using the select helper. Can it be set this way?
You'll need to add it to the array of values used by options_for_select instead of using the "prompt" option, which always sets a blank value. You should also make the array of sizes have two elements in each subarray: one for the displayed text and one for the value.
<% options = [["Please select a Print Size",0]] + #image_sizes.collect{ |i| [i.dimension, i.dimension] } %>
<%= f.select :image_size_id, options_for_select(options), id: 'image_size_select' %>
You can also try this.
<%= f.select :image_size_id, options_for_select([["Please select a Print Size", 0]] + #image_sizes.pluck(:dimension, :id)), {}, id: 'image_size_select' %>
Related
I want a result like this :
<select dir="rtl">
<option selected disabled>Choose a car</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
With the following code I can only end up with :
<%= f.select(:car, xxxxxx, {:include_blank => 'Choose a car', :disabled => 'Choose a car'}) %>
=>
<select id="xxx" name="xxx">
<option value="">Choose a car</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
The first option is not disabled...
Ito A's answer will not work in Rails 4.2 (Unsure about earlier versions).
From the documentation...
:disabled - can be a single value or an array of values that will be disabled options in the final output.
Therefore, the :disabled option should be given a value that matches the value of one of the options in the collection. So, :disabled => 'volvo' would disable the option with value='volvo'. However, it will not match the include_blank option, because that option is not part of the collection passed into the select method.
Rails select helper does not directly support the desired behavior. However, you can work around it by adding a blank option to the collection as follows.
Create the collection and then add a blank option to it.
car_names = %w(volvo saab mercedes audi)
car_names_with_blank = car_names.map{|c| [c, c]}.prepend(['Choose a car', nil])
In the view:
<%= f.select(:name, car_names_with_blank, {disabled: '', selected: ''}) %>
Here's a link to a Github repository with a working example. The example also shows that Ito A's answer and other answers to similar SO questions will not work.
UPDATED: I Have updated my answer with a solution and additional info.
I believe they add the functionality in Rails 6.
From the pull request:
Enable select tag helper to mark prompt option as selected and/or disabled for required field. Example:
select :post,
:category,
["lifestyle", "programming", "spiritual"],
{ selected: "", disabled: "", prompt: "Choose one" },
{ required: true }
Placeholder option would be selected and disabled. The HTML produced:
<select required="required" name="post[category]" id="post_category">
<option disabled="disabled" selected="selected" value="">Choose one</option>
<option value="lifestyle">lifestyle</option>
<option value="programming">programming</option>
<option value="spiritual">spiritual</option>
</select>
I believe what you are looking for is as below:
<%= f.select(:car, xxxxxx, {:include_blank => 'Choose a car', :disabled => 1}) %>
An application of the above noted solution follows.
create the application helper function
def build_selections(prompt: "Select One", selections: {})
selections.reverse!.push([prompt, nil]).reverse!
end
then use it in your view:
<%= f.select :category_id, build_selections(prompt: 'Select Category',
selections: #category.collect{|x| [x.name, x.id]}),
disabled: '' %>
I am using bellow code and function options_from_collection_for_select for generating options for user.
<%= select_tag 'receiver', options_from_collection_for_select(#user, 'id', 'email') %>
Above code generate bellow html:
<select id="receiver" name="receiver" style="display: none;">
<option value="1">email1#yahoo.com</option>
<option value="2">email2#gmail.com</option>
<option value="3">email3#gmail.com</option>
</select>
But i want email with username, e.g <optionvalue="1">email1#yahoo.com(some_user)</option>
Suggestion any alternate function or customize current function will be appreciated.
In your User model add a method :
def user_dispay_name
"#{email}(#{full_name})"
end
Now do :
<%= select_tag 'receiver', options_from_collection_for_select(#user, 'id', 'user_dispay_name') %>
But without options_from_collection_for_select this function i'm using below code:
<%= select_tag 'receiver', options_for_select(#user.map{ |c| ["#{c.email} (#{c.display_name})", "#{c.id}"] }) %>
I have something like this as a select_tag :
<p><%= setting_select :ui_theme, My::Themes.themes.collect {|t| [t.name, t.id]}, :blank => :label_default, :label => :label_theme %></p>
Rite now it is collecting all the values and displaying but I want to collect only a specific value and make it default. This value has name = "Test".
Thus it should look like this and it should be default:
<option selected="selected" value="Test">Test</option>
Note : Here setting_select is a helper which is defined like this:
def setting_select(setting, choices, options={})
if blank_text = options.delete(:blank)
choices = [[blank_text.is_a?(Symbol) ? l(blank_text) : blank_text, '']] + choices
end
setting_label(setting, options).html_safe +
select_tag("settings[#{setting}]",
options_for_select(choices, Setting.send(setting).to_s),
options).html_safe
end
options_for_select allows you to pre-select an option by passing its value. Example:
<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %>
output:
<option value="1">Lisbon</option>
<option value="2" selected="selected">Madrid</option>
More information about select tag and options_for_select.
I have an action that renders a view which contains this:
<select id ='dynamic_select'>
<option value = "<%= activity_path %>">All</option>
<option value = "<%= activity_path(:type => 'enrolled') %>">Enrolled</option>
<option value = "<%= activity_path(:type => 'redeem') %>">Redeem</option>
<option value = "<%= activity_path(:type => 'social') %>">Social</option>
</select>
What would be the correct/Rails way of rendering that select and mark as selected one of the options depending on the type parameter:
If there is no type parameter, select "All" options, if there is type=enrolled parameter select Enrolled option, and so on...
I have managed to do that client side with Javascript, but I am wondering what would be the Rails way of doing so.
Rendered HTML:
<select id="dynamic_select" name="dynamic_select">
<option value="/activity">All</option>
<option value="/activity?type=enrolled">Enrolled</option>
<option value="/activity?type=redeem">Redeem</option>
<option value="/activity?type=social">Social</option>
</select>
Something like the following. I have displayed the select_options here, but you should probably generate them in your controller and pass them through to the view.
The key is using options_for_select.
<% select_options = {"All" => activity_path} %>
<% %w{Enrolled Redeem Social}.each {|opt| select_options[opt] = activity_path(:type => opt.downcase)} %>
<% form_for(resource) do |f| %>
<%= s.select :dynamic, options_for_select(select_option, :selected => select_options[#default || "All"]) %>
<% end %>
Rails 2.3.5, Ruby 1.86
I haven't been able to figure this out. The 'onchange' in the select below is not being written (no onchange written in the HTML). I haven't seen a reference to the syntax being different except in some older examples the onchange is surrounded in brackets:
<%= f.select :directory_id, options_for_select(#directories, #directory_to_select), :onchange => 'folder_lookup()' %>
results in:
<select id="contact_directory_id" name="contact[directory_id]">
<option value="2">test_1</option>
<option value="4">test_2</option>
<option value="33" selected="selected">test_3</option>
</select>
If I simply change "f.select" to "select_tag" the onchange is written correctly (not that I want to do that though):
<%= select_tag :directory_id, options_for_select(#directories, #directory_to_select), :onchange => 'folder_lookup()' %>
results in:
<select id="contact_directory_id" name="directory_id" onchange="folder_lookup()">
<option value="2">test_1</option>
<option value="4">test_2</option>
<option value="33" selected="selected">test_2</option>
</select>
Am I missing a syntax difference for onchange between a select and select_tag helper?
Thanks!
This is what you want:
<%= f.select :directory_id, options_for_select(#directories, #directory_to_select), {}, :onchange => 'folder_lookup()' %>
With select the method signature looks like this select(object, method, choices, options = {}, html_options = {}). onchange is an html_option, since you don't have any options, you need an empty hash so that your last onchange is taken as an html_option.