Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fidalgo opened this issue Jun 23, 2016 · 9 comments · May be fixed by #1529
Open

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

fidalgo opened this issue Jun 23, 2016 · 9 comments · May be fixed by #1529

Comments

@fidalgo
Copy link

fidalgo commented Jun 23, 2016

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: 64d8c40

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

@moofkit
Copy link

moofkit commented Jul 12, 2016

same here

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

Database: PostgreSQL
Rails: 5.0.0
ruby 2.3.1

@luislezcair
Copy link

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

@doubleton
Copy link

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

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

@asaletnik
Copy link

Same here.

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

@mattwbarry
Copy link

Same here

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

@bitberry-dev
Copy link

Same bug here

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

Also ransack sorting ignores aliases.

@danielnolan
Copy link

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

@scarroll32
Copy link
Member

scarroll32 commented Aug 19, 2022

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>

rspeicher added a commit to rspeicher/ransack-689-test that referenced this issue May 23, 2023
@rspeicher
Copy link
Contributor

rspeicher commented May 23, 2023

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

def read_attribute(name)
if self[name].respond_to?(:value)
self[name].value
else
self[name]
end
end
-- 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants