Simple Drop Down List from Array in Rails - ruby-on-rails

This seems like it should be in Rails A1, but I can't find it any where. I have a bunch of locations stored in a Location table, made up of text fields City, County, Country etc. (This should have all been normalised out in my opinion, but there you go...).
A user can search by Location, and they enter into the separate search fields. At the moment, I'd like the Country such box to be a drop down list of countries. I currently get the list of countries in the db as follows:
#countries = Location.find(:all, :select=>"DISTINCT country")
The search box is currently just a text field:
<%= f.text_field :country,{:placeholder => "Country",:size=>20 } %>
But I'd like that to be a drop down box of the countries that are already in the #countries variable. It will just pass the country as a string. I'm a complete Newbie to Rails and even the basics are flumaxing me...Help!

If name is the actual name of the county in your country model:
<%= f.select(:country, #countries.map(&:name), {:include_blank => 'Select a Country'}) %>
You could do differently, having the value of the field different from the value displayed. Documentation lives here.
FYI, I usually use a very convenient gem named Carmen to handle country lists etc...

Related

Adding custom field in rails form helper

I'm working in a rails app and for country state and city select field I want to create a custom field so that It can be used later as already existed field.
For example
f.country_select :country
Above field should automatically generate a select with countries list.
I want three fields country, state and city so connected that when country field selected state select field gets state list and when state is selected city select works same.
Thank you for your valuable answer.
For this you should start from here . In this rails cast you can understand how to dynamically populate data in child(State) select box on basis of parent(Country) selection.
You need to extend functionality for cities same.
But in given tutorial you should have countries and state data. For managing that data you can use City-State.
In tutorial it is used like Country.find(:all), So you need to change it to CS.get. and for state of a selected country like CS.get :us.
Hope this works.
Yes, you can add this method to ActionView::Helpers::FormBuilder class
here is example for
class ActionView::Helpers::FormBuilder
def error_message_on(method, options = {})
#template.error_message_on(#object_name, method, objectify_options(options))
end
end
and then you can do this
= f.label :description, I18n.t(:description)
= f.text_field :description, class: 'form-control'
= f.error_message_on :description
You can use the gem city-state github gem link .

Rails Form multi-select list keyboard keys+display type and subtype

I have a form for #product that does multi-select list of product types(stored in database with has-many-through association between product-type.It works correctly by putting correct values in DB.
<%= f.collection_select(:type_ids, Type.all.order(:name), :id, :name,
{:selected => #product.type_ids, include_blank: false,:required => true},
{:multiple => true, size: 5})%>
But I have 2 issues:
I'm unable to use ctrl+click key for multi select. Only shift+click works. Any other method other than this or do I need to enable some keyboard/browser for Macbook Pro-version 10.12.2 & Chrome?
I have in the DB a column subtype(brand) for product. I want to display in the above a multi-select box showing both ptype and subtype columns from DB table Type as following: (I guess it's by grouped_options_for_select but can't find relevant example of retrieving from DB.)
Clothes -- M&S Clothes -- Next Clothes -- Monsoon
so chosen is what you are looking for :)

TWO dynamic dropdowns with ONE model

I have a search field where I want to add two dropdowns that should help me filter my search results. In my database I have one Product model with two columns, named country and type.
The first dropdown should filter by country and the second should filter by type. However, I am trying to set the second dropdown dynamically meaning that depending on the first dropdown (country) only those types should be displayed in the second dropdown that match the respective country selected. If possible this behavior should also work the other way around i.e. depending on the type, only matching countries should be displayed.
I've been trying several tutorials that work with more than one model however, I didn't manage to get it to work with only one model. My code for the dropdown selections is the following:
<%= form_tag("filter", :id => "filter_form", :method => "post") do %>
<label for="country" class="country">Country</label> <%= select_tag :country, options_for_select(Product.pluck(:country).uniq), { include_blank: 'Select country' }%>
<label for="type" class="type">Type</label>
<%= select_tag :type, options_for_select(Product.pluck(:type).uniq),{ include_blank: "Alle Kategorien" }%>
Is is possible with only one model?
Do I need AJAX for this or is Javascrip/jQuery sufficient?
Thank you very much!

How to show unique records in Rails?

There is a model named Company and it is having number of records. Later I added a field named area. I search field, i am adding this field too.
I am displaying all the areas in a drop down as follows:
<%= label_tag :area, "Area" %>
<%= select_tag 'area', options_for_select(Company.uniq.pluck(:area)),include_blank: true, class: 'form-control'} %>
Now areas are displaying fine but when I give area as "us" in one case and "US" in another case and "Us" in other case, it is displaying 3 fields
and also the previous records will be having area fields as empty/blank, here it is showing 2 or more blanks.
How to show unique records for capital and downcase and how to show only one blank in drop down?
Change this:
Company.uniq.pluck(:area)
to this:
Company.pluck(:area).compact.map(&:downcase).uniq
This will give you uniq downcased areas. i.e. you will get only us instead of three options: US, us and Us.

Rails: I want to limit the selections available in form.html.erb based on logic

I need to reduce the selectable items to what is appropriate considering what was previously established.
Say I have [Class] has_many [Order] has_many [Family]
(Remember old-school version of Kingdom Phylum Class Order Family Genus Species?)
When the data-entry person has previously selected, say, the four-legged mamals [Class],
and the Canids [Order] (I have no clue if this is at all right, but I hope you get the point),
They should just get Wolves, Foxes, and Dogs, etc. as their choices since they've narrowed it down that far.
I don't want to give the operator a list of [Families] like Cats or Crows or Crocodiles.
How do I go about that?
I'd assume you're trying to draw, say, the Family select box like this:
<%= collection_select :family_id, Family.all, :id, :name %>
But given that the user has already selected an order (let's assume you've instantiated that object as #order:
<%= collection_select :family_id, #order.families, :id, :name %>

Resources