Rails 如何制作过滤器 link
Rails How to make a filter link
我正在 rails 中进行分面搜索。它来自我之前的问题:Rails: How to use facets with search results
我能做的是使用范围来获取与搜索结果相关的所有方面,如图所示:
我目前正在做的是使用 facets 文本作为 link 来为我们已经得到的搜索结果添加额外的参数。
关于这个我有两个问题:
- How to take existing search URL and add a link to add/merge params when a link is clicked. I am using params merge which is not working.
The code is as below.
<h3 style = "color:#7C064D; text-align: center;"><strong>CYLINDER</strong></h3>
<%- @listings_cylinder.each do |key, value| -%>
<h5 style = "color:#F00000; text-align: left;"><strong><%= link_to "#{key.upcase if key} (#{value})" , search_listings_path(params.merge(:cylinder => "#{key}")) %> </strong></h5>
<% end %>
- How to use multiple filters at the same time. What I want is when a facet link is clicked, it should add a small filter link below the
sorting link in the pic. When a user wants to undo that facet filter
effect, he clicks the X on that small link and he gets to the search
query without the filter. The same what happens on hotels.com or
priceline.com as shown:
注意: 我不想使用 solr/elaticsearch 因为它不是这里的最佳用例。我想从头学起。我已经能够使用简单的 rails 构建构面文本。有一点帮助,我就能做到。
代码:
我正在使用的范围:
scope :listing_search_cities, -> {group(:city).count}
scope :listing_search_body_type, -> {group(:bodytype).count}
scope :listing_search_states, -> {group(:state).count}
scope :listing_search_newused, -> {group(:NewUsed).count}
scope :listing_search_transmission, -> {group(:transmission).count}
scope :listing_search_cylinder, -> {group(:cylinder).count}
scope :listing_search_fuel, -> {group(:fuel).count}
scope :listing_search_drive, -> {group(:drive).count}
scope :listing_search_trim, -> {group(:trim).count}
scope :listing_search_color, -> {group(:color).count}
scope :listing_search_year, -> {group(:year).count}
scope :listing_search_interiorcolor, -> {group(:interiorcolor).count}
在控制器搜索操作中,我使用:
def search
@listings = Listing.search(params)
@listings_cities = @listings.listing_search_cities
@listings_bodytype = @listings.listing_search_body_type
@listings_states = @listings.listing_search_states
@listings_newused = @listings.listing_search_newused
@listings_cylinder = @listings.listing_search_cylinder
@listings_transmission = @listings.listing_search_transmission
@listings_drive = @listings.listing_search_drive
@listings_trim = @listings.listing_search_trim
@listings_fuel = @listings.listing_search_fuel
@listings_color = @listings.listing_search_color
@listings_interiorcolor = @listings.listing_search_interiorcolor
@listings_year = @listings.listing_search_year
end
在视图中,我通过这样的键值使用迭代:
<%- @listings_cylinder.each do |key, value| -%>
<h5 style = "color:#F00000; text-align: left;"><strong><%= link_to "#{key.upcase if key} (#{value})" , search_listings_path(params.merge(:cylinder => "#{key}")) %> </strong></h5>
<% end %>
不是解决方案,而是一些想法(未测试):
将所有过滤器传递给参数散列中的搜索方法(例如 params[:filters])。
定义一个包含所有过滤器的实例变量,并将其加载到控制器的搜索方法中。
根据过滤器过滤列表。
def search
@current_filters = params[:filters]
@listings = Listing.all
@listings = @listings.where(:cylinder => @current_filters[:cylinder]) if @current_filters[:cylinder]
@listings = @listings.where(:fuel => @current_filters[:fuel]) if @current_filters[:fuel]
#etc
end
在视图中:
<% @listings_cylinder.each do |key, value| %>
<h5><%= link_to "#{key.upcase if key} (#{value})", search_listings_path(filters: @current_filters.merge(:cylinder => "#{key}")) %>
</h5>
<% end %>
在视图中,当显示当前过滤器时:
Applied filters:
<ul>
<% @current_filters.each do |key, value| %>
<li><%= link_to value, search_listings_path(filters: @current_filters.except(key)) %>
</li>
<% end %>
</ul>
我正在 rails 中进行分面搜索。它来自我之前的问题:Rails: How to use facets with search results
我能做的是使用范围来获取与搜索结果相关的所有方面,如图所示:
我目前正在做的是使用 facets 文本作为 link 来为我们已经得到的搜索结果添加额外的参数。
关于这个我有两个问题:
- How to take existing search URL and add a link to add/merge params when a link is clicked. I am using params merge which is not working. The code is as below.
<h3 style = "color:#7C064D; text-align: center;"><strong>CYLINDER</strong></h3>
<%- @listings_cylinder.each do |key, value| -%>
<h5 style = "color:#F00000; text-align: left;"><strong><%= link_to "#{key.upcase if key} (#{value})" , search_listings_path(params.merge(:cylinder => "#{key}")) %> </strong></h5>
<% end %>
- How to use multiple filters at the same time. What I want is when a facet link is clicked, it should add a small filter link below the sorting link in the pic. When a user wants to undo that facet filter effect, he clicks the X on that small link and he gets to the search query without the filter. The same what happens on hotels.com or priceline.com as shown:
注意: 我不想使用 solr/elaticsearch 因为它不是这里的最佳用例。我想从头学起。我已经能够使用简单的 rails 构建构面文本。有一点帮助,我就能做到。
代码:
我正在使用的范围:
scope :listing_search_cities, -> {group(:city).count}
scope :listing_search_body_type, -> {group(:bodytype).count}
scope :listing_search_states, -> {group(:state).count}
scope :listing_search_newused, -> {group(:NewUsed).count}
scope :listing_search_transmission, -> {group(:transmission).count}
scope :listing_search_cylinder, -> {group(:cylinder).count}
scope :listing_search_fuel, -> {group(:fuel).count}
scope :listing_search_drive, -> {group(:drive).count}
scope :listing_search_trim, -> {group(:trim).count}
scope :listing_search_color, -> {group(:color).count}
scope :listing_search_year, -> {group(:year).count}
scope :listing_search_interiorcolor, -> {group(:interiorcolor).count}
在控制器搜索操作中,我使用:
def search
@listings = Listing.search(params)
@listings_cities = @listings.listing_search_cities
@listings_bodytype = @listings.listing_search_body_type
@listings_states = @listings.listing_search_states
@listings_newused = @listings.listing_search_newused
@listings_cylinder = @listings.listing_search_cylinder
@listings_transmission = @listings.listing_search_transmission
@listings_drive = @listings.listing_search_drive
@listings_trim = @listings.listing_search_trim
@listings_fuel = @listings.listing_search_fuel
@listings_color = @listings.listing_search_color
@listings_interiorcolor = @listings.listing_search_interiorcolor
@listings_year = @listings.listing_search_year
end
在视图中,我通过这样的键值使用迭代:
<%- @listings_cylinder.each do |key, value| -%>
<h5 style = "color:#F00000; text-align: left;"><strong><%= link_to "#{key.upcase if key} (#{value})" , search_listings_path(params.merge(:cylinder => "#{key}")) %> </strong></h5>
<% end %>
不是解决方案,而是一些想法(未测试):
将所有过滤器传递给参数散列中的搜索方法(例如 params[:filters])。
定义一个包含所有过滤器的实例变量,并将其加载到控制器的搜索方法中。
根据过滤器过滤列表。
def search
@current_filters = params[:filters]
@listings = Listing.all
@listings = @listings.where(:cylinder => @current_filters[:cylinder]) if @current_filters[:cylinder]
@listings = @listings.where(:fuel => @current_filters[:fuel]) if @current_filters[:fuel]
#etc
end
在视图中:
<% @listings_cylinder.each do |key, value| %>
<h5><%= link_to "#{key.upcase if key} (#{value})", search_listings_path(filters: @current_filters.merge(:cylinder => "#{key}")) %>
</h5>
<% end %>
在视图中,当显示当前过滤器时:
Applied filters:
<ul>
<% @current_filters.each do |key, value| %>
<li><%= link_to value, search_listings_path(filters: @current_filters.except(key)) %>
</li>
<% end %>
</ul>