Afternoon,
I'm finding Ransack a little confusing. I'm trying to populate my dropdown with only the values available and filter the index based on that but when I try with various options it pulls all the information into the dropdown including duplicates.
How can I create this form so that it shows only what is available and does the search based on that.
jobs_controller.rb
def index
@jobs = Job.all
@show_sub_nav = true
@q = Job.search(params[:q])
@searches = @q.result(distinct: true)
@lang = Job.find_by_sql("SELECT languages FROM jobs GROUP BY languages").map &:languages
end
_subnav.html.erb
<%= search_form_for @q do |f| %>
<%= f.select "languages", options_for_select(@lang) %>
<%= f.submit %>
<% end %>
I currently get this error:
No valid predicate for languages
Thanks for the help, I have real trouble trying to understand collection_select and these form_helpers as I don't find the API documentation very helpful so any links would be useful as well.
In Ransack, you need to add a predicate onto the field name. If you want to do an exact match against the option that is chosen you can do something like:
<%= f.select :language_eq, @lang %>
For more on predicates, see the Basic Searching page. Additional info is available in RailsCast 370.