Ransack搜索表单将搜索替换为“占位符", [英] Ransack search form replaces search with "Placeholder"

查看:91
本文介绍了Ransack搜索表单将搜索替换为“占位符",的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的城市搜袋搜罗表格在某种程度上具有默认城市"字样,即使我单击提交"并且URL中存在参数,我仍然会返回具有默认城市的选择,这也会影响搜索结果.这是我的选择(我使用select2).

My ransack serach form for cities somehow has a "default city" and even if i click on submit and params are present in the url, I still come back to a select with a default city, which also affects the search results. Here is my select (I work with select2).

= search_form_for @search, url: filter_path do |f|
    .columns.is-centered{style: 'padding-top: 100px;'}
        .column.is-half
            = f.select(:geo_code_eq, options_for_select(@collection, (params[:q][:geo_code_eq].nil? ? '' : params[:q][:geo_code_eq])), {:include_blank => true}, {class: "city-select2 select-input" })
            = f.submit "Search"

我想它很薄.与收集有关,但我无法弄清楚,这个默认城市的确切原因是什么.所以这部分控制器:

I guess it has smth. to do with collection, but I couldn't find out, what's exactly bringing this default city. So this part of controller:

def filter
 @city_list = city_list
 params[:q] ||= {}
 params[:q][:geo_code_eq] = @city_list[0][1]
 if params[:q].present? && params[:q][:geo_code_eq].present?
  @city = Geo::City.find_by(geo_code: params[:q][:geo_code_eq]) 
  @collection = [["#{@city}", "#{params[:q][:geo_code_eq]}"]]
end
    @search = @city.streets.ransack(params[:q])
    @streets = @search.result
end

推荐答案

这是将占位符放入选择参数中:[:q] [:geo_code_eq] = @city_list [0] [1],基本上说总是有第一个列表中的位置是我选择的位置.我用一个空集合替换了它,并使用了Rails .none来显示没有街道的情况,而且情况也很好.

This was putting the placeholder in the select params[:q][:geo_code_eq] = @city_list[0][1], basically saying there is always first sity in the list to be places in my select. I replaces it with an empty collection and a case where no street is to show just yet using rails .none and it wored just fine.

这篇关于Ransack搜索表单将搜索替换为“占位符",的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆