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.
Related
I'm building an app where a user can customize components in a pc(system), based on components that are available in their respective tables (eg. motherboards table, cpus table.. etc)
I would like the user to be able to select the components via a dropdown select in the system form.
I have been able to achieve this by using collection_select like this
<%= collection_select(:system, :motherboard_id, Motherboard.all, :id, :model, prompt: true) %>
However, the collection_select displays all components in the table, and I wish to only display those components that have an available: true attribute.
I have tried
<%= collection_select(:system, :motherboard_id, Motherboard.any? {|mobo| mobo.available?} , :id, :model, prompt: true) %>
which results in undefined method 'map' for false:FalseClass screenshot:
I thought about adding a before_save callback that checks each items availability, but if that's not the only way to get it to work, I think that would be a poor decision in terms of UX
You can use
<%= collection_select(:system, :motherboard_id, Motherboard.where(available: true), :id, :model, prompt: true) %>
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 can not figure out why this is not working, I saw a couple of examples and seems to be right.
I have two clases that are related, consultant and salary (a salary belongs_to a consultant).
The problem I have is, when I want to edit a salary, the consultant that appears on the form is not bind to the select (in the select it just appears the list as if I was creating a new one)
<%= f.select :consultant_id, options_for_select(Consultant.active.map{|s|[s.first_name, s.id]}, params[:consultant_id]), {}, class: 'form-control' %>
I think you should check your route that the :consultant_id param is available on this page. Else you may need to change the params[:consultant_id] to something like salary.consultant_id
Alternatively, you can use the collection_select method as such:
f.collection_select(:consultant_id, Consultant.active, :id, :first_name, prompt: true, class: 'form-control')
Let me know whichever works for you.
Note: It's not best practice referring to domain object within your view.
I have created dropdown language selection list, however I would like to make it stick when user edits. I know that it can be done with the second argument of select_tag but could not do it.
I have a user model and associated language model;
<%= f.label :language, "Spoken Languages" %>
<br>
<%= select_tag("user[language_ids][]", options_for_select(Language.all.collect { |ff| [ff.name, ff.id] }, #user.languages.all.collect { |kk| [kk.name, kk.id] }),
{:multiple=>true, :class => "language_select form-control"}) %>
EDIT:
Here how it looks like, even though spoken languages are set before;
But it should look like with the pre selected languages (comes from database);
I used Select2 to create the dropdown list.
Try changing this part to: #user.languages.collect { |kk| kk.id }
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