Ransack Gem Rails and multiselect - ruby-on-rails

I am using the ransack gem for my rails app and have a problem with multiselect.
I have a job and a company model, a company has many jobs, a job belongs to a company.
I want to perform a search on jobs so people can see jobs from specific companies,
I have done some research and apparently this should work in the search form:
= search_form_for #q do |f|
= f.collection_select :company_name_cont, Company.all, :id, :name, {:multiple => true}, class: 'chosen-it'
= f.submit "search"
So in the view I get the list (with autocomplete from the chosen gem) but when I click on search it returns no result and I can't select multiple companies.
However, when I use a search field instead like this:
= f.search_field :company_name_cont
The search works.
Could you help me guys
Thanks a lot

You should use company_name_in instead of company_name_cont:
= f.collection_select :company_name_in, Company.all, :id, :name, {}, {:multiple => true}
More info can be found in ransack's wiki here: https://github.com/activerecord-hackery/ransack/wiki/Basic-Searching#in

In general, we can write this as
= f.collection_select :some_id_in, Model.all, :id, :name, {}, {:multiple => true}
where some_id is a model attribute for which we have multiple select value for filter.

Related

Selecting multiple strings with single dropdown in Rails

I am building a Rails app that tracks musicians. I want each user to be able to to select multiple genres they like from a form when they create a profile. I want to store it within the "profile" table for the user in the "genre" column. Right now, I have this, but it only allows me to select ONE option. I want the dropdown to allow multiple options. Is that possible? I'm sure this is very simple.
<%= simple_form_for #profile do |f| %>
<%= f.input :genre, collection:['Rock','Blues','Jazz','Classical','Soul','R&B','Alternative', 'Other'], label: "Favorite Genres" %>
Right now, genre is a string. Do I need to pass an array? How would I do that?
There are 2 things you need to do to handle a multi-select with rails.
1) Allowing the user to select multiple options on the form with the {:multiple => true} option.
<%= simple_form_for #profile do |f| %>
<%= f.input :genre, collection:['Rock','Blues','Jazz','Classical','Soul','R&B','Alternative', 'Other'], label: "Favorite Genres", input_html: {:multiple => true} %>
2) Let your controller accept an array of params for the value.
def profile_params
params.require(:profile).permit( :years_spent_playing, :user_id, :name, :bio, :age, :city, :state, :primary_instrument, :second_instrument, :third_instrument, :status, :looking_for, :image, :genre => [])
end
Im not sure your setup but I get the feeling you may not have the proper models and associations setup to handle this in the ideal way. I would suggest looking at adding a has_many :through association and breaking your genres out into a separate model and table.

Rails form dropdown using two text_method

I am creating a form has a drop down selection. I want to use two "text_method"s for the input but I am unsure how to do this. I want to include the year and name (both are two different columns in my rails model.
Here is what I have but it does not work:
<%= f.collection_select :bat_id, Bat.all, :id, :model_year, :model_name, include_blank: true %>
Here is the official documentation- http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
Use this in your view:
<%= f.collection_select :bat_id, Bat.all, :id, :model_year_and_name, include_blank: true %>
Add a method like this to your Bat model:
def model_year_and_name
"#{model_year} #{model_name}"
end

Rails + Ransack - Drop Down List Collection?

I am loving the ransack gem for its flexibility, however I am unable to get the standard collection_select to function properly. Perhaps someone can assist.
Example:
<%= collection_select(:expense, :project_id, Project.all,
:id, :name, { prompt: 'Select Project'}, { class: 'span4' }) %>
in this case, this code is from an expense entry screen, so the first parameter is the expense object. What should it be in the ransack form? Also, I know I need to get the suffix in there. In this example, I would like project_id_eq to be the search pattern.
Also, my form is on a controller and view called "reports", I am not putting this search in the default controllers.
Thanks!
Seems that this will work.
<%= f.select :project_id_eq, options_from_collection_for_select(Project.all, "id", "name", #search.project_id_eq) %>
If anyone has another suggestion, would love to know it too.
To do this with an include_blank, put it outside of the parentheses:
ex:
<%= f.select :languages_id_eq, options_from_collection_for_select(Language.all, "id", "name"), include_blank: true %>
== UPDATE ==
better yet, use f.collection_select. This way after the user searches for something using the drop down, that option is preselected on the following page:
<%= form.collection_select :vendor_id_eq, Vendor.all, :id, :name, include_blank: true %>

How to use select method in form_for in Rails

I would like to use a select method in form_for in Rails. I'm not quite sure how to do it. Could anyone point me to an example?
Are you talking about select or select_tag or collection_select?
# advanced users only
= f.select :field_id, options_for_select(Model.collect{|m| [m.name, m.id]})
# easiest, assuming you have a model
= f.collection_select :field_id, Model.all, :id, :name
# without model
= select_tag 'model[field_id]', #model.field_id, options_for_select(Model.collect{|m| [m.name, m.id]})

How to include a "Please select..." (default/prompt) in a grouped dropdown list?

The code I am using for the dropdown list is this:
<%= f.select :post_type_id, option_groups_from_collection_for_select(#categories, :post_types, :name, :id, :name) %>
It neatly divides the options into optgroups.
But how do I modify the code to include a prompt (or a default value) of "Please select..." ?
It seems hard to do with grouped dropdowns.
(The rails docs seem to suggest using a hash, but I've tried several alternatives without success.)
Bah, right after I posted the question I found the answer was in the docs for select, and not under option_groups_from_collection_for_selectdocs where I had been looking.
The answer is:
<%= f.select :post_type_id, option_groups_from_collection_for_select(#categories, :post_types, :name, :id, :name), :include_blank => "Please select..." %>

Resources