ransack
ransack copied to clipboard
Ransack form search field value not persisted between requests when using alias
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
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
Any update on this? I'm having the same problem.
If it helps anyone, I worked around this using value
option:
= f.search_field :quick_search_cont, value: params.dig(:q, :quick_search_cont)
Same here.
Ransack: 1.8.3 / 1.8.2 Database: MySQL Rails: 4.2.9 Ruby: 2.3.3
Same here
Ransack: 1.8.3 Database: PostgreSQL Rails: 4.0.13 Ruby: 2.3.3
Same bug here
Ransack: 2.1.1 Database: MySQL Rails: 5.2.2 Ruby: 2.5.3
Also ransack sorting ignores aliases.
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
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>
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.