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.
Related
Still pretty new to ruby.. How would I add a feature to this drop down to allow users to enter an attribute that is not in the drop down list. Right now when I try to add something it says no results matched.
<%= f.select(:attribute_type, #attribute_types, {include_blank: true}, class: 'form-control selectpicker', data: {'live-search' => 'true'}) %>
The no results matched message is because you are not adding something to the select, but you are searching in the select list.
If you want users to have the option to fill in whatever they want instead of selecting it drop the select you can just add an input field below the select.
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!
I want to send two values with my collection select. Currently, i'm saving the ID but I want to have access to the name in my controller as well.
= f.collection_select :foo_id, #foos, :id, :name
That's my code, pretty simple. Just having trouble getting access to that name.
The collection also comes from a external API, so I don't want to have to touch the API again to get the name.
As, I said in the comment. You can customize the <option> tag values like :
= f.collection_select :foo_id, #foos, ->(ob) { "#{ob.name}|#{ob.id}" }, :name
Now, inside the controller just split it the value on |, and use it.
This is just an idea out of millions.
The background is this:
our end users are not the most tech suave so we try to make it as simple as possible for these guys.
We have a form where a reviewer will look at a record and qualify it. This data goes into multiple columns:
example:
Was it a lead?
based on the answer it'll bring yup 2 select tags that'll go to the following database column:
yes:
<td><%= f.select :reasoninquiry_id, options_from_collection_for_select(#inquiryreasons, 'id', 'reason', #lead.reasoninquiry_id), { include_blank: true } %></td>
No:
<%= f.select :nonleadaction_id, options_from_collection_for_select(#nonleadactions, 'id', 'non_lead_action_type', #lead.nonleadaction_id), { include_blank: true } %>
We give our end users the opportunity to update this if they want. And here is where it gets tricky.
Currently our app is written in PHP and we use 1 select box and it sends data to 2 different tables, depending on what was selected.
Is there a way I can do this in rails?
To be clear what we'd want is 1 select box that will be able to submit data to 2 database columns, depending on what was selected. I'd want one select tag for 2 collections.
The select box would show options for both #inquiryreasons (which would submit to :reasoninquiry_id if one of them is selected) and #nonleadactions (which would submit to :nonleadactions_id if one of them is selected.)
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...