1

目标是打印出每个子类别/子类别中的内容。

我有一个类别列表,然后是子类别。子类别和内容具有 has_many :through 关系。我需要能够点击一个或多个子类别,然后显示相关内容。不确定几件事:

1)我是制作一个子类别控制器并在那里构建过滤器还是通过内容控制器?

2)我应该只做一个自定义路线吗?

这就是我现在所拥有的。

目录

  create_table "contents", :force => true do |t|
    t.string   "title"
    t.text     "short_description"
    t.text     "long_description"
    t.datetime "published_date"
    t.datetime "edited_date"
    t.string   "read_length_time"
    t.string   "tag"
    t.integer  "author_id"
    t.integer  "contenttype_id"
    t.string   "collection"
    t.integer  "collection_id"
    t.string   "subcategory"
    t.integer  "subcategory_id"
    t.boolean  "published"
    t.datetime "created_at",               :null => false
    t.datetime "updated_at",               :null => false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.string   "infographic_file_name"
    t.string   "infographic_content_type"
    t.integer  "infographic_file_size"
    t.datetime "infographic_updated_at"
    t.string   "video"
  end

子类别表

      create_table "subcategories", :force => true do |t|
        t.string   "title"
        t.integer  "category_id"
        t.string   "content"
        t.integer  "content_id"
        t.datetime "created_at",  :null => false
        t.datetime "updated_at",  :null => false
      end

join table
create_table "subcats_contents", :id => false, :force => true do |t|
    t.integer "subcategory_id"
    t.integer "content_id"
  end

  add_index "subcats_contents", ["content_id"], :name => "index_subcats_contents_on_content_id"
  add_index "subcats_contents", ["subcategory_id", "content_id"], :name => "index_subcats_contents_on_subcategory_id_and_content_id"
  add_index "subcats_contents", ["subcategory_id"], :name => "index_subcats_contents_on_subcategory_id"

路线.rb

  match 'contents/filter_association/:id' => 'contents#filter_association', :as => 'filter_association', :via => :get

内容控制器

  def filter_association
    @subcategory = Subcategory.find_by_id(params[:id])
    Subcategory.where.merge(-> { joins(:content_id) })

    respond_to do |format|
      format.html
      format.json { render json: @content }
    end
  end

这是单击子类别的视图:

<table>
  <% @subcategories.each do |subcategory| %>
    <tr>
      <td><%= subcategory.title %></td><br/>
      <td><%= check_box_tag (:subcategory) %></td>
    </tr>
  <% end %>
<%= submit_tag "submit" %>
<table>

错误:参数数量错误(0 代表 1)

4

1 回答 1

0

你得到了一个错误,因为你没有将任何参数传递给 where 方法。

如果我理解正确,您会从用户表单中收到一些子类别列表,并且您想要获取与这些子类别相关的所有内容的列表。

Content.joins(:subcategories).where(:subcategories => {:id => <variable with subcategory ids>}).group(:content => :id)
于 2013-09-09T21:55:47.730 回答