How to push extra text to 'select' label in Ruby on Rails - ruby-on-rails

I have a select_field on a form such as:
<%= f.select(:task_id, Task.all.collect {|p| [p.name, p.id]}, {prompt: "Select"}, {class: 'form-control', required: true}) %>
The generated HTML is:
<select class="form-control" required="required" name="company[task_id]" id="company_task_id">
<option value="">Select</option>
<option value="3">Site Management</option>
<option value="1">Real Estate</option>
<option value="2">Meeting</option>
<option value="4">Training</option>
</select>
I want to add some extra text to the label such as:
Site Management (ABC)
Real Estate (ABC)
Meeting (ABC)
Training (ABC)
How do I add (ABC)?

Define a method in the Task model which will do that for you
#task.rb
def name_with_abc
"#{self.name}" + "(ABC)"
end
And now change the select to
<%= f.select(:task_id, Task.all.collect {|p| [p.name_with_abc, p.id]}, {prompt: "Select"}, {class: 'form-control', required: true}) %>

For more organised way It can be done as : -
in controller -
#tasks_options = Task.distinct.pluck(<<-PLUCK, :id)
CONCAT_WS("", tasks.name, " (ABC)")
PLUCK
which will fire a sql query
SELECT CONCAT_WS("", tasks.name, "(ABC)"), `tasks`.`id` FROM `tasks`
In view
<%= f.select(:task_id, #tasks_options, {prompt: "Select"}, {class: 'form-control', required: true}) %>
So instead of using collect which will iterate each element of array , this can be better solution.

<%= f.select(:task_id, Task.all.collect {|p| ["#{p.name} (ABC)", p.id]}, {prompt: "Select"}, {class: 'form-control', required: true}) %>

Related

Using select2's selected option value in a rails forms field

How do I assign the selected option value:
<select class="select2-simple-dropdown">
<% Season.all.each do |season| %>
<option id="chosen-season" value="<%= season.id %>"><%= season.name %></option>
<% end %>
</select>
To a form's field, let's say: Voyage.given_season ?
Use the rails select field instead and do it like this
<%= f.select :season_id, Season.all.pluck(:name, :id), {},
{ class: 'select2-simple-dropdown'} %>
Hope this helps.
If you want your select input to accept multiple options, you can pass multiple: true
<%= f.select(:season_id, Season.all.collect {|m| [ m.name, m.id] }, class: "form-control select2-simple-dropdown", id: "list-markets", multiple: true) %>
https://aalvarez.me/posts/select2-with-simple-form-in-rails/

How can I add classes to a simple_form input wrapper in Rails?

I need to add a data attribute to an f.association input in a simple form. To do this I found this article and subsequent reference to simpleform's documentation. It is easy enough to add classes with the standard syntax, I am wondering why it's proving difficult using a wrapper.
So rather than the standard:
<%= f.association :size, input_html:{class: 'sku-suffix-component'} %>
I am using:
<%= f.input :size, as: :select, input_html:{class: 'sku-suffix-component'} do %>
<%= f.select :size, Size.all.map{ |s| [s.name, s.id, {'data-code-num' => s.code_num}] } %>
<% end %>
Much to my frustration, the added class is nowhere in the <select> tag (not to mention the missing other default classes and id naming convention [id="product_size_id" becomes id="product_size"]). I have tried various syntaxes and placements of the input_html hash.
Below you can see the difference in appearance etc. On the right is using the standard syntax.
And here is the resulting html:
<select name="product[size]" id="product_size"> (...)
Compared with:
<select class="form-control select required sku-suffix-component" name="product[color_id]" id="product_color_id"> (...)
Try this solution and if it is possible than create some link for jsfiddle so we can test and try to find solution
<%= f.input :size do %>
<%= f.select :size, Size.all.map { |s| [s.name, s.id, { class: 'sku-suffix-component', 'data-code-num' => s.code_num }]}, input_html: { class: 'sku-suffix-component' }, include_blank: true %>
<% end %>
I found the solution combining strategies that I found on separate posts.
The long and the short of it was to utilize the map method but not within a custom input wrapper.
<%= f.association :size, collection: Size.all.map{ |s| [s.name, s.id, {'data-code-num' => s.code_num}]}, input_html:{class: 'sku-suffix-component'} %>
This nets perfectly my desired <select> with an added class, and the <option>'s with a custom data- attribute.
<select class="form-control select required sku-suffix-component" name="product[size_id]" id="product_size_id">
<option value=""></option>
<option data-code-num="001" value="113">onesize</option>
(...)

Rails Select Helper for Forms with Data and HTML attributes

I'm trying to create a Select element using Rails form_for helper. This is what I'm trying to create:
<label for="charge_occurrence">When to charge</label>
<select class="w-select" data-name="charge_occurrence" id="charge_occurrence" name="charge_occurrence" required="required">
<option value="monthly">Monthly</option>
<option value="episodic">Per episode</option>
</select>
I have tried several variations of this:
<%= f.select :charge_occurrence, [['Monthly', 'monthly'], ['Per Episode', 'episodic']], data: {name: 'charge_occurrence'}, html: {class: "w-input", maxlength: "256", required: "required"} %>
I can get everything to work except for the data-name part.
Try <%= f.select :charge_occurrence, [['Monthly', 'monthly'], ['Per Episode', 'episodic']], html: {'data-name' => 'charge_occurence', class: "w-input", maxlength: "256", required: "required"} %>
See: How to add data attribute in Rails form select tag?

Rails form_for multiple collection_select, values not selected on form failure

I'm not sure why, but my form is not showing the options selected on submit, even though the params hash shows that the information is being returned to the page.
Collection select code:
<%= f.collection_select :post_topic_ids, PostTopic.all, :id, :name, {}, { multiple: true, class: 'form-control' } %>
Which renders:
<select multiple="multiple" class="form-control" name="post[post_topic_ids][]" id="post_post_topic_ids">
<option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option>
</select>
Params returned after form validation error
params = {"post"=>{"post_topic_ids"=>["", "1"]}}
Update
I have also tried:
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }), multiple: true %>
and:
<%= select_tag 'post_topic_ids', options_from_collection_for_select(PostTopic.all, "id", "name"), multiple: true %>
Which renders:
<select name="post_topic_ids[]" id="post_topic_ids" multiple="multiple"><option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option></select>
you need to specify which element is selected a third parameter
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }, --->selected_element<---), multiple: true %>
look at http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select for some examples.

Add value to select in rails

i have a select that take values from database.
<%= select_tag :location, options_from_collection_for_select(Country.all, :id, :country_name), { :class => 'selectpicker' } %>
So i get all countries from database.
How i can do for add a custom value(for example Any, with value 0), to this select list taken from database ?
For example now i have:
<select>
<option value="UK">UK</option>
</select>
and i want get this:
<select>
<option value="0">Any</option>
<option value="UK">UK</option>
</select>
Thanks.
You can use the either prompt or include_blank options (FormOptionsHelper) as follows:
<%= select_tag :location, options_from_collection_for_select(Country.all, :id, :country_name), :prompt => 'Any', :class => 'selectpicker' %>

Resources