Adding input with simple_form to get <datalist> - ruby-on-rails

I am trying to use simple_form to generate an input textfield with collections, equal to
<input list="browsers" name="browser">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
something to be like:

Apparently simple_form has discussed adding this feature and decided currently not to implement it (February 2019).

What about:
<%= form.input :browsers, input_html: { list: "browsers" } %>

You can try this
<%= f.select :browsers, ["Internet Explorer", "Firefox", "Chrome", "Opera", "Safari"]%>

Related

how to show selected in f.select in rails4 view

Hi I am having this in my view
<%= f.select :role,:collection => Employee.roles.keys%>
I want to show selected in this i.e #employee[:role]. When I give this
<%= f.select :role,:collection => Employee.roles.keys, :selected => #employee[:role] %>
then i get this type of list
<select id="employee_role" name="employee[role]">
<optgroup label="collection">
<option value="super_admin">super_admin</option>
<option value="hr">hr</option>
<option value="admin">admin</option>
<option value="inventory">inventory</option>
<option value="employee">employee</option>
</optgroup>
<optgroup label="selected">HR</optgroup>
</select>
I don't know how to show my selected value. Please guide in solving this. Thanks in advance.
You can use this following code.
<%= f.select :role,Employee.roles.keys, :selected => #employee[:role] %>

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(...)) %>

Syntax for options_from_collections_for_select in rails 4

I'm trying to add a class to the select drop down populated using options_from_collection_for_select
Here's the snippet I'm working with:
<%= select_tag "instructor", options_from_collection_for_select(#instructors, "id", "full_name"), :include_blank => true %>
Where do html_options go in this instance?
Desired result:
<select id="example_select" name="example_select" class="example_class">
<option value></option>
<option value="1">Example option 1</option>
<option value="2">Example option 2</option>
</select>
I've tried to follow syntax for options_for_select and have only found questions relating to that tag specifically; it doesn't work as I'd expect.
Any advice for this question, and for select tag templating questions like this in general would be appreciated.
The answer is actually simple:
<%= select_tag "instructor", options_from_collection_for_select(#instructors, "id", "full_name"), :include_blank => true, :class => "example_class" %>
Will produce:
<select id="example_select" name="example_select" class="example_class">
<option value></option>
<option value="1">Example option 1</option>
<option value="2">Example option 2</option>
</select>
Thanks Jesse Goodfellow
Reference: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select

Can't generate Chosen select using Rails helpers

I have styled beatiful select, but I need to connect it to database, so I have simple html code:
<select class="chzn-select" tabindex="1" style="width:300px;" data-placeholder="Choose a Category">
<option value=""></option>
<option value="Fashion">Fashion</option>
<option value="Coupon">Coupon</option>
<option value="Sport">Sport</option>
</select>
and code using rails form helpers:
<%= f.select :category, options_for_select(%w[Fashion Health Travel Food Coupons]), :class => "chzn-select"%>
EDIT: Add html code generated by rails :
<select id="website_category" name="website[category]"><option value="Fashion">Fashion</option>
<option value="Health">Health</option>
<option value="Travel">Travel</option>
<option value="Food">Food</option>
<option value="Coupons">Coupons</option></select>
It shows me blank page, however code is generating due to I see it in my Google console.
I should set all other attributes or what is porblem ?
What does you generated code look like? Can you post that?
If you're not generating any options, maybe try:
<%= f.select :category, options_for_select(["Fashion", "Health", "Travel", "Food", "Coupons"]), :class => "chzn-select"%>
I was needed to call it correctly from js file:
$("#website_category").chosen();

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