20

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!

Atari2600
  • 1,219
  • 2
  • 13
  • 26

2 Answers2

36

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.

Atari2600
  • 1,219
  • 2
  • 13
  • 26
  • This works fairly well with the exception that it doesn't allow you to set a prompt ("Select"). – dspencer Jun 28 '13 at 20:15
  • 3
    @dspencer Just put the `include_blank: true` outside of the parentheses: `<%= f.select :languages_name_eq, options_from_collection_for_select(Language.all, "id", "name"), include_blank: true %>` – bigpotato Feb 05 '14 at 20:14
  • can you please tell how to add eq when we use option for select method and we are calling this from helper – gangothri Oct 03 '19 at 12:20
24

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 %>
bigpotato
  • 26,262
  • 56
  • 178
  • 334