How to add selected attribute in f.select helper in rails? - ruby-on-rails

i have written the f.select helper for this code
<select>
<option>abc</option>
<option>def</option>
<option selected>ghi</option>
</select>
<%= f.select(:xFields, ['abc', 'def', 'ghi]) %>
How do i make option 'ghi' selected as default?

edit
try this:
<%= f.select options_for_select(:xFields, ['abc', 'def', 'ghi'], 'ghi') %>
the following should work
<%= f.select(:xFields, [['ghi'], 'abc', 'def']) %>
based on Rails 3: f.select - options_for_select

This is working
<%= f.select(:xFields, options_for_select( ['abc', 'def', 'ghi'], 'ghi')) %>

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 to get selected value from a dropdown on click in ruby on rails?

I am new to ruby on rails please help me with this simple code :
I need to print
'HI' if option 1 is selected
'BONJOUR' if option 2,
'HOLA' if option 3,
'NAMASTE' if option 4.
on selecting a value on the dropdown and click the button,
<%= form_for :person do |f| %>
<%= f.select :desired_attribute, ['option1','option2','option3','option4']%>
<%= button_to "Show Text" , :onclick => "show_back(), return false" %>
<% end %>
involve your server.
view. Add remote: true to your form. And handle ajax request in controller. more about ajax in rails
<%= form_for :person, remote: true do |f| %>
<%= f.select :desired_attribute, options_for_select([ ['option1', '1'],['option2', '2'],['option3', '3'],['option4', '4']]) %>
<%= button_to "Show Text" , :onclick => "show_back(), return false" %>
<% end %>
in your controller
class PersonsController
MY_HASH = { '1': 'HI', '2': 'BONJOUR', '3': 'HOLA', '4': 'NAMASTE' }
def name_of_action
respond_to do |format|
format.js { render 'your_partial_name', name: MY_HASH[params[:person][:desired_attribute]] }
end
end
your_partial_name.js.erb
alert("<%= name %>");
Instead of MY_HASH it's better to use I18n to handle language issues. more about I18n
Use this syntax:
<%= f.select :point1, options_for_select([ ['option1', 'HI'],['option2', 'BONJOUR'],['option3', 'HOLA'],['option4', 'NAMASTE']]) %>
you will dropdown like this:
<select id="point1">
<option value="HI">option1</option>
<option value="BONJOUR">option2</option>
<option value="HOLA">option3</option>
<option value="NAMASTE">option4</option>
</select>
Dropdown option map with its value then you will get the value.

how do i get the id of the selected item in dropdown

<%= form_for(:offer,:url=>{:controller=>'offers',:action=>'combo'}) do |f|%>
<%= f.select :catId_get, options_from_collection_for_select(#categories, "id", "name"), prompt: "Select Category" %>
I am new in rails.I have a dropdown where all categories are there.When i select a category from this dropdown i want to get its category id in my controller,so that i can use that id for it's child dropdown.
Select
Each select option in HTML has two values -- the value and the label:
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
It's only the value which is passed to your controller. This means if you are able to create the select tag in your Rails app with the correct value / label setup, it will pass the correct data you require.
Rails
Here's how I'd handle it:
<%= form_for :offer, offers_combo_path do |f|%>
<%= f.collection_select :cat_id, #categories, :id, :name, prompt: "Select Category" %>
This will pass the following params to your categories_controller:
#app/controllers/categories_controller.rb
class CategoriesController < ApplicationController
def combo
params[:offer][:cat_id]
end
end
Recommendation
I'd actually recommend you use the form_tag helper for this, rather than form_for. Reason being that form_for is mainly for ActiveRecord objects, and although you can use :symbols in the helper, you will really need to use a much less elaborate system
I'd just replace your form_for with the following:
<%= form_tag offer_combo_path do %>
<%= collection_select :cat_id, #categories, :id, :name, prompt: "Select Category" %>
<% end %>
Your id should be accessible by
params[:offer][:catId_get]
in your controller.

Setting HTML options for <option> tags in select_tag

In the following ERB:
<%= form_for #my_model do |f| %>
<%= f.select :my_attribute, options_from_collection_for_select(#my_options, :attribute, :name), {class: "select-style"} %>
<% end %>
The HTML options at the end will be applied to the generated <select> tag in the injected HTML. How can I supply HTML style options that will be applied to the injected <option> tags?
The Rails documention explains how to do this when using options_for_select, but not for when using options_from_collection_for_select.
Interesting question. Where do you get the html options from?
Say, you want a static class for all options:
<%= f.select :my_attribute, options_from_collection_for_select(#my_options.map{|o| [o.attribute,{class: 'select-style'},o.name], :first, :last) %>
of cause, you can set the class from an attribute of my_option.
The trick is, that options_from_collection_for_select also extracts html options from each element (the Hash), so you have to construct the elements to include this hash.
But then, you can as well use options_for_select (as options_from_collection_for_select does internally):
<%= f.select :my_attribute, options_for_select(#my_options.map{|o| [o.name,{class: 'select-style'},o.attribute]) %>
In my opinion this option is unavailable.
you can do this:
<%= options_from_collection_for_select(#my_options.map { |o| [o.attribute, o.name, { class: 'option-style' }] }, :first, :second) %> - but its ugly.
just do it with options_for_select:
<%= options_for_select(#my_options.map { |o| [o.attribute, o.name, { class: 'option-style' }] }) %>

Populating Select Box with Hash Array in rails

Anyway I've created a Hash Array :
#example= ['A' => '1', 'B' => '2']
or Array
#example=[1,2]
here you've got the key (being the english name) and the value (the
generic equivalent).
what I want to do is make a drop-down single-selection box use these
key/value properties to render it correctly,
<%= f.collection_select #example %>
...this doesn't work but what i'd like is generate the form code...
#from HASH
<select >
<option value="1">A</option>
<option value="2">B</option>
</select>
or
#from array
<select>
<option>1</option>
<option>2</option>
</select>
Any help really appreciated.
For:
#example = [["A", 1, {:class=>"bold"}], ["B", 2], ["C", 3]] # {:class=> "bold"} is optional, use only if you need html class for option tag.
Try:
<%= form_for #whatever do |f|%>
# some code here..
<%= f.select :example, options_for_select(#example) %>
# rest of the code..
<% end %>
Or you can use:
<%= form_for #whatever do |f|%>
# some code here..
<%= f.select :example, #example %> # I am guessing that maybe you can not pass hash here for option tag.
# rest of the code..
<% end %>
Without form_for:
<%= select_tag :example, #example %>
# or
<%= select_tag :example, options_for_select(#example)%>

Resources