Search code examples
ruby-on-rails-3.2ransack

Default attribute_select and predicate_select using ransack


I'm using the Ransack gem in Rails and would like to show a default attribute select (e.g. 'name' pre-selected on the dropdown) and also a default predicate (e.g. 'contains'). The idea is to allow the user to quickly query the user model by entering a users name without the need to select 'name' and 'contains' everytime.

The code snippet below is pretty standard and shows my ransack view setup.

<%= f.attribute_fields do |a| %>    
  <%= a.attribute_select %>
<% end %>
<%= f.predicate_select compounds: false%>
<%= f.value_fields do |v| %>    
  <%= v.text_field :value, :class => "page_filter_value_field" %>
<% end %>

Any help would be much appreciated ...


Solution

  • Just revisited this. Couldn't find anything on the Ransack side of things, maybe I missed something. A quick nasty solution is to change the select using jquery:

    $(function(){
      $("#q_c_0_a_0_name option[value=unit_title]").attr("selected","selected") ;
      $("#q_c_0_p option[value=cont]").attr("selected","selected") ;
    });
    

    q_c_0_a_0_name is the id of the attribute_select dropdown where I set the value to be the option value and q_c_0_p is the predicate_select dropdown.

    This only sets the defaults for the first instance of the filter, so, when you add more filter fields they are blank