Collecting only a specific value and selecting as default - ruby-on-rails

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.

Related

Rails generate select with both options and option groups

Is it possible to generate select like:
<select>
<option value>Some nill value label</option>
<option value="1">Option one</option>
<optgroup label="Option Group 1" class="css-class-for-option-group-1">
<option value="1:1">Option one in group 1</option>
<option value="1:2">Option two in group 1</option>
</optgroup>
<optgroup label="Option Group 2" class="css-class-for-option-group-2">
<option value="2:1">Option one in group 2</option>
<option value="2:2">Option two in group 2</option>
</optgroup>
</select>
I tried to use the grouped_options_for_select, but I failed to find I way to pass both option and groups to it. Is there any way to achieve this? I am open to suggestions using SimpleForm as well.
Thanks!
This is hard to answer precisely without knowing what the data looks like, but I'm going to guess that you have something like this:
#grouped_options = [
["Some nil value label"],
["Option one", "1"],
["Option Group 1",
[
["Option one in group 1", "1:1"],
["Option two in group 1", "1:2"],
]
],
["Option Group 2",
[
["Option one in group 2", "2:1"],
["Option two in group 2", "2:2"],
]
],
]
With that, you have a couple options. In plain ERB, you can do it like this:
<%= select "thing", "some_attr" do %>
<% #grouped_options.each do |label, value_or_options| %>
<% if Array === value_or_options %>
<%= tag.optgroup options_for_select(value_or_options), label: label %>
<% else %>
<%= tag.option label, value: value_or_options %>
<% end %>
<% end %>
<% end %>
Personally, though, I would write a helper. The Enumerable#chunk method splits an array into runs of values that return the same thing for the given block, making it easy to separate the grouped from the non-grouped items so we can use grouped_options_for_select and options_for_select, respectively:
def ungrouped_and_grouped_options_for_select(choices, selected_key = nil)
capture do
choices
.chunk {|_, choice_or_group| Array === choice_or_group }
.each do |is_group, choices_or_grouped_choices|
if is_group
concat grouped_options_for_select(choices_or_grouped_choices, selected_key)
else
concat options_for_select(choices_or_grouped_choices, selected_key)
end
end
end
end
You could then use it this:
<%= select "thing", "some_attr" do %>
<%= ungrouped_and_grouped_options_for_select(#grouped_options) %>
<% end %>
You can see both of these approaches in action on repl.it: https://repl.it/#jrunning/UnacceptablePlaintiveServer (see views/tests/index.html.erb, controllers/tests_controller.rb, and helpers/application_helper.rb).
Assuming that the data is formatted as described by Jordan, you could also combine both 'select options' methods (options_for_select and grouped_options_for_select) by summing their return, like so:
<%= f.select :selected_ids, options_for_select([['Option 1', 1]]) + grouped_options_for_select([['Option Group 1', ['group1opt1', 11], ['group1opt2', 12]], ['Option Group 2', ['group2opt1', 21], ['group2opt2', 22]]]), {}, { class: 'class' } %>

Rails Form select : preselect one choice

I have this rails code :
<% status_a = [ ["DRAFT", "DRAFT"], ["OPEN", "OPEN"], ["CLOSE", "CLOSE"] ] %>
<%= form_for(:dash_action, url: brokers_dashboard_path ) do |f| %>
<%= f.select(:select_status, options_for_select(status_a), {}, selected:'OPEN' %>
<% end %>
When it runs, it generates this HTML code :
<select selected="selected" name="dash_action[select_status]" id="dash_action_select_status">
<option value="DRAFT">DRAFT</option>
<option value="OPEN">OPEN</option>
<option value="CLOSE">CLOSE</option>
...
But what I expect is :
selected="OPEN" and not "selected"
Why the select method is not doing what I want ?
Try following code snippet, default value should be the parameter of options_for_select
f.select :select_status, options_for_select(status_a, 'OPEN')

Set a value for prompt in select Rails

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' %>

Option Group in select tag using rails

I am getting values of #templates in my controller.
right now I have structure like this:
<%= select_tag :template ,options_from_collection_for_select(#templates,"id", "name"),{:prompt => "--Built In Templates--",:class=>"form-control m-b-sm required"}%>
But now I want to add option group in my select_tag
how can I create structure something like:
<%= select_tag :template ,grouped_options_for_select(['Built-In Templates',#templates.collect{|v| [v.name, v.id ] }],#templates),{:prompt => "please select",:class=>"form-control m-b-sm required"}%>
I want to create array of option group.like
<select>
<optgroup label="Built-In Templates">
<option value="id_default">default</option>
</optgroup>
<optgroup label="Custom Templates">
<option value="id_my template">my template</option>
</optgroup>
</select>
I have added one field called type in template model.Type field has radio button built_in and custom so when I create one template we choose radio button of that type.
Now, my dropdown for option select is look like this:
<%= select_tag :template ,grouped_options_for_select([['Built-In templates', #built_in.collect {|v| [ v.name, v.id ] }],['Custom', #custom.collect {|v| [ v.name, v.id ] }]]),{:prompt => t("please_select"),:class=>"form-control m-b-sm required"}%>

Selected attribute depending on params value with Rails

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 %>

Resources