Options for Select, stick at selected Value - ruby-on-rails

I'm trying to get the User's Selection to stick in the options_for_select.
Heres my code:
<div class="form-group">
<%= f.label :team_for, "I'm for" %> <br>
<%= f.select :team_for, options_for_select(User::TEAMFOR), :prompt => "Please Select" %>
</div>
How can i set this up?
My HTML output:
<div class="form-group">
<label for="user_team_for">I'm for</label> <br>
<select id="user_team_for" name="user[team_for]">
<option value="">Please Select</option>
<option value="Team Druid">Team 1</option>
<option value="Team Hunter">Team 2</option>
<option value="Team Mage">Team 3</option>
<option value="Team Paladin">Team 4</option>
<option value="Team Priest">Team 5</option>
<option value="Team Rogue">Team 6</option>
<option value="Team Shaman">Team 7</option>
<option value="Team Warrior">Team 8</option></select>
</div>
If i select something, it gets into the Database and is shown correctly, but on the Edit Page the selection is not shown, instead always "Team 1" is set as selected.
I need the selected value to be shown.

The 2nd param to options_for_select is the selected value: http://guides.rubyonrails.org/form_helpers.html#option-tags-from-a-collection-of-arbitrary-objects
The second argument to options_for_select must be exactly equal to the desired internal value. In particular if the value is the integer 2 you cannot pass "2" to options_for_select — you must pass 2. Be aware of values extracted from the params hash as they are all strings.
Try this:
<%= f.select :team_for, options_for_select(User::TEAMFOR, #user.team_for), :prompt => "Please Select" %>

Related

Rails 5.2 - Going From Select To Radio Buttons

In my ERB view, I have the following two types of select inside a form.
<%= f.select :shape, Diamond.shapes.map { |k,v| [k.humanize, v] },{:include_blank => ''},{:class => "form-control", :required => true} %>
<%= select_tag "term[]", options_for_select(Diamond.colors.map { |k,v| [k.upcase, v] }), class: "form-control" %>
I am wanting to change it into a radio button or just button and cannot figure out how to code it correctly. The options I have are an enum saved in Diamond.shapes.
Here are the 2 HTML element.
<select class="form-control" required="required" name="wholesale[shape]" id="wholesale_shape"><option value=""></option>
<option value="1">Asscher</option>
<option value="2">Emerald</option>
<option value="3">Heart</option>
<option value="4">Marquise</option>
<option value="5">Oval</option>
<option value="6">Pear</option>
<option value="7">Princess</option>
<option value="8">Radiant</option>
<option value="9">Round</option>
<option value="10">Cushion</option>
</select>
<select name="term[]" id="term_" class="form-control"><option value="14">D</option>
<option value="15">E</option>
<option value="16">F</option>
<option value="17">G</option>
<option value="18">H</option>
<option value="19">I</option>
<option value="20">J</option>
<option value="21">K</option>
<option value="22">L</option>
<option value="23">M</option>
<option value="24">N</option></select>
Any ideas? I have been trying radio button collections but havent figured out the correct coding and am not even sure if that is the best way to go. Thank you.
You can do it like this:
<% Diamond.colors.keys.each do |color| %>
<%= f.radio_button :term, color %>
<%= f.label color.to_sym %>
<% end %>

Thymeleaf 3 - Data-th-text cannot contains "Not" - Could not parse as expression: "Not" exception

I have a simple select form with three options. I want one of the to be "Not mounted"
The problem is that I cannot set "Not" in data-th-textbecause it is recognized as an expression.
<div class="form-group">
<label for="status">Status:</label>
<select data-th-field="*{status}" id="status" class="form-control">
<option data-th-value="available" data-th-text="Available"></option>
<option data-th-value="broken" data-th-text="Broken"></option>
<option data-th-value="not mounted" data-th-text="Not Mounted" ></option>
</select>
</div>
I receive an expesion
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "Not" (template: "exhibitor/editExhibitor" - line 20, col 60)
How can I show the "Not Mounted" text ?
If you're using data-th-text, then thymeleaf is expecting an expression that can be evaluated. You have 2 options here:
Use regular attributes.
<div class="form-group">
<label for="status">Status:</label>
<select data-th-field="*{status}" id="status" class="form-control">
<option value="available">Available</option>
<option value="broken">Broken</option>
<option value="not mounted">Not Mounted</option>
</select>
</div>
Make your attributes valid thymeleaf expressions.
<div class="form-group">
<label for="status">Status:</label>
<select data-th-field="*{status}" id="status" class="form-control">
<option data-th-value="'available'" data-th-text="'Available'"></option>
<option data-th-value="'broken'" data-th-text="'Broken'"></option>
<option data-th-value="'not mounted'" data-th-text="'Not Mounted'" ></option>
</select>
</div>
or
<div class="form-group">
<label for="status">Status:</label>
<select data-th-field="*{status}" id="status" class="form-control">
<option data-th-value="${'available'}" data-th-text="${'Available'}"></option>
<option data-th-value="${'broken'}" data-th-text="${'Broken'}"></option>
<option data-th-value="${'not mounted'}" data-th-text="${'Not Mounted'}" ></option>
</select>
</div>
You can make sure that Thymeleaf understands it's a string by using
single quotes:
data-th-text="'Not Mounted'"
Or even better, make it internationalized and get it from a message bundle:
data-th-text="#{notMounted}"
In your messages.properties:
notMounted=Not Mounted
but then this could alternatively be:
<option th:text="#{notMounted}" value="not mounted">Not Mounted</option>
Just note that whichever option you chose, you'll want to escape any single-quotes within your string with a backslash \.

How set "selected" in option ROR

in _form.html.erb file I wrote:
<%= f.number_field :tov_id %>
and that code produce this html :
<input type="number" value="3" name="price[tov_id]" id="price_tov_id" />
this code:
<%= select_tag(:tov_id, options_for_select(#tovs,2), :prompt => "select tov" ) %>
make this html
<select name="tov_id" id="tov_id">
<option value="">select tov</option>
<option value="1">brake pad</option>
<option selected="selected" value="2" >nut</option>
<option value="3">bolt</option></select>
as we can see : the second parameter to option_to_select respond to selected option
But when I wrote this:
<%= select_tag(:tov_id, options_for_select(#tovs , :tov_id ), :prompt => "Select tov" ) %>
no any selected appear :(
<select name="tov_id" id="tov_id"><option value="">Select tov</option><option value="1">brake pad</option>
<option value="2">nut</option>
<option value="3">bolt</option></select>
How can I get the value from :tov_id and put it to options_for_select ?

Rails: generate select box for form

I want to make a select box for a collection. I use method options_for_select but It just generates <option></option> field, not <select></select> outside.
Here is my code:
<% categories_array = Category.all.map{|category| [category.name, category.id]} %>
<%= options_for_select(categories_array) %>
And here is the result:
<option value="5483c910485559047a000000">Programming</option>
<option value="5483c921485559047a010000">Business</option>
<option value="5483c92b485559047a020000">Game Programming</option>
But I expected:
<select id = "categoryId">
<option value="5483c910485559047a000000">Programming</option>
<option value="5483c921485559047a010000">Business</option>
<option value="5483c92b485559047a020000">Game Programming</option>
</select>
Moreover, I can get this value when publish this form to server. How can I do this
Thanks :)
You can see how select and options helpers are combined here:
http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease
As noted there:
<%= select_tag(:city_id, options_for_select(...)) %>

select option tag in ror?

i am new at ror.
option select tag in rails
<%= select_tag(:city_id, '<option value="1">Lisbon</option>,<option value="2">Madrid</option>' %>
produces this html:
<select id="city_id" name="city_id">
<option value="1">Lisbon</option>
<option value="2">hjkLisbon</option>
</select>
and option select tag
<%= select_tag(:city_id, options_for_select([['Lisbon', 1], ['Madrid', 2]])) %>
produces this html:
<select id="city_id" name="city_id">
<option value="1">Lisbon</option>
<option value="2">Madrid</option>
</select>
for second option select tag i am getting the dropdown with options but in first one i am not getting it, only dropdown is there in first one but no options.
can anybody please explain me why it is so?
If you only have these two cities you can use normal html
<select id="city_id" name="city_id">
<option value="1">Lisbon</option>
<option value="2">Madrid</option>
</select>
If you fetch the cities from you database you can do
select_tag ‘city_id’, options_for_select(#cities.collect{ |u| [u.name, u.id] })
This would generate something like:
<select id="city_id" name="city_id">
<option value="1">Lisbon</option>
<option value="2">Madrid</option>
</select>
Do not be afraid to use html. The most simple solution is the best solution.
Ref this
Change
<%= select_tag(:city_id, '<option value="1">Lisbon</option>,<option value="2">Madrid</option>' %>
To
<%= select_tag(:city_id, '<option value="1">Lisbon</option>,<option value="2">Madrid</option>') %>
You are not closing the select tag i.e. ) is missing
if you really want to keep selected the country name on edit page then..
<%= f.select :country, (country_name), :selected => #model.country %>
Where country_name is a method in helper.

Resources