【问题标题】:Filter results on index page based on has_many through根据 has_many 过滤索引页上的结果
【发布时间】:2015-01-03 01:27:36
【问题描述】:

我已经尝试过像this 这样的解决方案,但我是Rails 初学者,在通过Pragmatic Studio Rails I & II Course 之后才构建我的第一个应用程序

基本上,我想建立一个简单的列表网站,如BookYogaRetreats.com 我试图弄清楚如何实现过滤器逻辑。到目前为止,我有一个列表模型、类别和分类。

上市模式:

has_many :categorizations, dependent: :destroy
has_many :categories, through: :categorizations

has_attached_file :image, styles: { :medium => "200x200#" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/

validates :title, presence: true, uniqueness: true

paginates_per 15

scope :by_category, lambda { |category| includes(:categories).where(:categories => { :name => category }) }

分类模型:

belongs_to :listing
belongs_to :category

类别模型:

has_many :categorizations, dependent: :destroy
has_many :listings, through: :categorizations

我的 Listings Controller 索引操作看起来像这样(我已经做了很多实验......)

class ListingsController < ApplicationController
  before_action :find_listing, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]
  before_filter :load_data, only: [:index]

def index
  @listings = @listings.includes(:categories).where(categories: { id: @category}) if @category_ids
  @listings = @listings.page(params[:page])

  respond_to do |format|
    format.js
    format.html
  end  
end

在我的 jumbotron 部分中,包括过滤器下拉列表,我有以下内容:

<div class="col-sm-6 col-md-4">
  <%= select_tag("listing[category_id]", options_from_collection_for_select(@categories, "id", "name") , { :prompt => 'Select a Category' }) %>
<%= submit_tag nil, class: "form_submit", method: "get" %>
</div> 

我的列表/index.html.erb 如下所示:

<% @listings.each_slice(3) do |listings|%>
  <div id="listings">
  <div class="row">
    <% listings.each do |listing|%>
      <div class="col-md-4">
        <div class="listing">
          <div class="image_wrapper">
            <%= link_to listing do %>
              <%= image_tag listing.image.url(:medium) %>
            <% end %> 
            <h2><%= link_to listing.title, listing %></h2>
            <p><%= listing.start_date %> - <%= listing.end_date %></p> 
          </div>   
        </div>    
      </div>
    <% end %>
  </div>
</div>  

在过去的两天里,我一直在尝试解决问题,并尝试使用 Filterific Gem 和 Ransack。我真的不想使用宝石,目前我不明白我需要做什么。我可以使用类别过滤器,单击提交按钮后会重新加载显示属于该特定类别的所有列表的页面?拥有 has_many through 关系可能不会让事情变得更容易。

我希望能够有类似 Listings?by_category=seminars 的东西,然后重新加载列表视图 index.html.erb 并仅显示包含类别研讨会的列表。

我可能完全走错了路。任何指针将不胜感激。非常感谢。

【问题讨论】:

  • 有人可以帮忙吗?

标签: ruby-on-rails ruby activerecord


【解决方案1】:

设法在这里找到解决方案:

我的看法:

<%= form_tag listings_path, :controller => 'listings', :action => 'index', :method => 'get', :id => "category_select" do %>
  <%= collection_select( nil, :category_id, Category.all, :id, :name, { :include_blank => 'All Categories'} , { :class => 'form-control' })  %>
  <%= submit_tag nil, class: "form_submit" %>
<% end %>

我的控制器索引操作:

@listings = Listing.includes(:categorizations).page(params[:page])

if params[:category_id].blank?
  @listings
else
  @category_id = Category.find_by(id: params[:category_id])
  @listings = @category_id.listings
end

respond_to do |format|
  format.js
  format.html
end  

这将允许我使用 /listings?category_id=5 并解决问题。如果您有任何更优雅的提示,请告诉我。

【讨论】:

    猜你喜欢
    • 2018-10-24
    • 2011-10-12
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    相关资源
    最近更新 更多