ransack icon indicating copy to clipboard operation
ransack copied to clipboard

[Question] The sort_link does not refer to the search criteria in the object passed in the first argument.

Open lobin-z0x50 opened this issue 1 year ago • 0 comments

Suppose you write sort_link as follows.

<th><%= sort_link @search_form, :address_1 %></th>

At this time, sort_link is not looking at the search criteria in @search_form, but is looking at the params in the controller to create the query string for the link. Therefore, even if the controller generates default search conditions, etc. and sets them in @search_form, the sort_link will not reflect them.

A workaround is to set the search condition to params[:q] as follows.

  def index
    @search_form = Facility.ransack(search_params)
    @search_form.sorts = ["ID asc"] if @search_form.sorts.empty?

    ...
  end

  private
    def search_params
      h = params.fetch(:q, default_search_params).permit(
          :groups_id_eq,
          :prefecture_id_eq,
          :address_1_start,
          :shelter_kind_eq,
          :facility_kind_eq,
          :s,
          :s => []
          )

      params[:q] = h
    end

Is this the intended design? We would like to avoid changing params if possible. Is there any other way?

lobin-z0x50 avatar Dec 20 '23 07:12 lobin-z0x50