ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Ransack form search field value not persisted between requests when using alias

Open fidalgo opened this issue 8 years ago • 9 comments

When I search, using an alias I cannot get the value of the searched parameter in the field. In the name field I have tested and it's working fine. My code: In controller:

  def index
    @q = Customer.ransack(params[:q])
    @customers = @q.result(distinct: true)
  end

In model:

  ransack_alias :quick_search, :name_or_phone_or_mobile_or_email_or_vat_identification

In view:

      <%= search_form_for @q do |f| %>
      <%= f.search_field :quick_search_cont, class: "form-control"%>
    </div>
    <div id="table-filter" class="well filter-well">
      <div class="form-group">
        <%= f.label (:name) %>
        <%= f.search_field :name_cont, class: "form-control" %>
      </div>

Ransack version: GIT remote: git://github.com/activerecord-hackery/ransack.git revision: 64d8c402e6e79e8ec2966a15c35b98196813ef8e

I'm using Ransack directly with: Database: PostgreSQL Rails: 5.0.0.rc1 ruby 2.2.5

fidalgo avatar Jun 23 '16 18:06 fidalgo

same here

Ransack version: GIT remote: git://github.com/activerecord-hackery/ransack.git revision: fd94685abc0d18d30a730a57fb3dbd8aff048f7d

Database: PostgreSQL Rails: 5.0.0 ruby 2.3.1

moofkit avatar Jul 12 '16 07:07 moofkit

Any update on this? I'm having the same problem.

luislezcair avatar Nov 13 '16 22:11 luislezcair

If it helps anyone, I worked around this using value option:

= f.search_field :quick_search_cont, value: params.dig(:q, :quick_search_cont)

doubleton avatar Mar 06 '17 15:03 doubleton

Same here.

Ransack: 1.8.3 / 1.8.2 Database: MySQL Rails: 4.2.9 Ruby: 2.3.3

asaletnik avatar Sep 23 '17 00:09 asaletnik

Same here

Ransack: 1.8.3 Database: PostgreSQL Rails: 4.0.13 Ruby: 2.3.3

mattwbarry avatar Oct 10 '17 20:10 mattwbarry

Same bug here

Ransack: 2.1.1 Database: MySQL Rails: 5.2.2 Ruby: 2.5.3

Also ransack sorting ignores aliases.

bitberry-dev avatar Feb 06 '19 21:02 bitberry-dev

This is happening for me also when using ransack with simple form, using an alias doesn't populate my checkboxes when the page refreshes after submitting the form. If I remove the alias everything works as expected Ransack: 3.1.0 Rails: 7.0.2.3 Ruby: 3.1.2 Simple Form: 5.1.0

danielnolan avatar May 12 '22 16:05 danielnolan

This approach is also needed to keep values for a select

<div class="form-group">
    <%= f.label :due_days_eq, t('Review_days') %>
    <%= f.select :due_days_eq, options_for_select([0,1,2,3,4,5], params.dig(:q, :due_days_eq)), { include_blank: t('Any') }  %>
</div>

scarroll32 avatar Aug 19 '22 22:08 scarroll32

Just ran into this today as well. Created https://github.com/rspeicher/ransack-689-test to offer a minimal reproduction of the issue, and a failing system test. The full_search alias is defined here.

I was able to trace it as far as https://github.com/activerecord-hackery/ransack/blob/dbffa8bed3e4cad9d7f51cd9233466ae4b68fb4d/lib/ransack/nodes/grouping.rb#L180-L186 -- in our example the full name is name_or_description_cont, but it's aliased as full_search_cont, but because the group naming uses the underlying, expanded name we get nothing back for the value.

I wonder if it would be possible to store the alias name alongside the group so either will return the value we need, or to have the form builder expand the alias before asking for the value.

rspeicher avatar May 23 '23 20:05 rspeicher