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!
Related
I have a dropdown menu:
<%= f.collection_select :price, Print.all, :printprice, :sizetotal, {prompt: "Pick A Size & Medium"} %>
It currently populates the :price database column with the value :printprice, and shows the value :sizetotal in the actual dropdown selection.
I want to be able to populate two database columns, one of which is the :price column which is already working, but I also want to populate another column called :size with the value :printsize. I want to do this using the same dropdown select menu.
Something like:
<%= f.collection_select :price, :size, Print.all, :printprice, :printsize, :sizetotal, {prompt: "Pick A Size & Medium"} %>
But obviously the above doesnt work
Is this possible?
Following my comment, here is the ID alternative. Basically you want to send only the ID of the print, and find again the whole object on the backend during the #create or #update actions
print_for_size_and_price = Print.find(params[:print_id_for_size_and_price])
So on the frontend I'd use something like this
select_tag(
:print_id_for_size_and_price,
options_from_collection_for_select(Print.all, :id, :sizetotal)
)
And in the backend I'd do somewhere
my_instance.price = print_for_size_and_price.printprice
my_instance.size = print_for_size_and_price.sizetotal
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.
I've two models in my app that are joined together using a many -to-many association (both "has an belongs to" other model). Browsing on internet I've seen that there's thousands of examples that shows how set up view and controller, but all of these use checkbox_tag inside view, while I need a dropdown menu in my view, since objects that I've to display are more than 100, so you understand why I cannot use checkbox.
Have you experienced the same problem? In witch way do you have build view?
What you'd want to do is use a multi-select, so instead of a checkbox like so:
<%= check_box_tag "product[category_ids][]", category.id, #product.categories.include(category) %>
You'd want to do
<%= select_tag "product[category_ids][]", options_from_collection_for_select(#categories, "id", "name"), :multiple => true %>
The :multiple => true is the important part to convert it to a multi-select box.
In my app, there are two models: rfq and standard. Their relationship is many-to-many. In rfq creating screen, the code below displays a list of available for selection in drop down list:
<%= simple_form_for #rfq do |f| %>
<%= f.association :standards, :collection => Standard.active_std.all(:order => 'name'), :label_method => :name, :value_method => :id %>
<% end %>
The problem is that the list is not collapsed, which means there are multiple standards displayed in a multi-line boxes. How can I reduce the box to one line only?
Thanks.
UPDATED: here is the screen shot of multiple line list box:
It's creating a multi-select because one rfq can have many standards, so it allows you to ctrl-click to select many standards.
You could try adding :input_html => { :size =>'1' } but I'm not sure that will preserve the scrollbar. It definitely won't drop down.
Here's someone else who wanted to do the same thing: HTML muliple select should look like HTML select. One of the answers refers to a Dropdown Check List implemented in jQuery, but that would take some work to integrate with SimpleForm.
SimpleForm has a very helpful Google Group--you might get more ideas there:
http://groups.google.com/group/plataformatec-simpleform
You can add as: :collection_select
Use
=f.collecion_select, model_associated_ids, collection, value, label
in your is like this
=f.collection_select, :standard_ids, Standard.active_std.all, :id, :name
you can find more info here
https://github.com/plataformatec/simple_form
A card can have one of many card_types. There are two models, card and card_type, where card_type is an [id, card_type_desc] pairing.
When you define a new card, you have to pick a card-type from a drop down list.
I have the list rendering correctly with the below collection_select box, but the new card.card_type_id field is NULL. How do you set it to the value from the list?
<%= collection_select(:card_type, :id, #card_types, :id, :card_type_desc) %>
Thanks in advance.
I am guessing, It should be...
<%= collection_select(:card, :card_type_id, #card_types, :id, :card_type_desc) %>