0

こんにちは、関連付けに少し問題があります。Rails 3.2 を使用しています。特別なブログを作成したいと考えています。このブログには多くのセクションがあり、このセクションには多くの記事があり、記事はカテゴリに属しています。だから私のモデルは次のとおりです。

class Article < ActiveRecord::Base
  belongs_to :section
  belongs_to :category

class Category < ActiveRecord::Base
  has_many :articles

class Section < ActiveRecord::Base
  has_many :article

したがって、記事の belongs_to セクションの routes.rb :

 resources :sections do
   resources :articles
 end

レーキ ルート:

                     POST   /sections/:section_id/articles(.:format)          articles#create
 new_section_article GET    /sections/:section_id/articles/new(.:format)      articles#new
edit_section_article GET    /sections/:section_id/articles/:id/edit(.:format) articles#edit
     section_article GET    /sections/:section_id/articles/:id(.:format)      articles#show
                     PUT    /sections/:section_id/articles/:id(.:format)      articles#update
                     DELETE /sections/:section_id/articles/:id(.:format)      articles#destroy
            sections GET    /sections(.:format)                               sections#index
                     POST   /sections(.:format)                               sections#create
         new_section GET    /sections/new(.:format)                           sections#new
        edit_section GET    /sections/:id/edit(.:format)                      sections#edit
             section GET    /sections/:id(.:format)                           sections#show
                     PUT    /sections/:id(.:format)                           sections#update
                     DELETE /sections/:id(.:format)                           sections#destroy

だから私の質問は、インデックスとショーアクションで Categories_controller を作成するにはどうすればよいですか。そのカテゴリに属する​​記事を表示し、記事パスのビュー (Category#show) に link_to を設定します。

4

2 に答える 2

1

ドメイン モデルに適合するためにネストされたルートが必要であると仮定すると (たとえば、記事がカテゴリまたはセクションのコンテキストで表示されているかどうかに基づいて、記事を異なる方法で表示する必要がある)、次のようにネストされたルートの別のセットを作成できます。 :

ルート.rb

resources :categories, only: [:index, :show] do
  resources :articles
end

これにより、カテゴリと記事をカテゴリ別に検索するルートが設定されますが、ArticlesControlleronparams[:category_id]などでロジックをフォークする必要がありますparams[:section_id]

class ArticlesController < ApplicationController
  def index
    if params[:section_id]
      # handle section_articles_path to display articles by section
    elsif params[:category_id]
      # handle category_articles_path to display articles by category
    else
      # handle articles_path to display all articles (assuming you have resources :articles in routes)
    end
  end
  # ...
end

カテゴリに基づいて記事を表示するリンクは、作成されたルート ヘルパー メソッドを使用します。

カテゴリ/show.html.erb

<ul>
  <% @category.articles.each do |article| %>
    <li><%= link_to article.title, category_article_path(@category, article) %></li>
  <% end %>
</ul>

もちろん、ビュー コードをリファクタリングして、コレクションを手動で反復するのではなく、独自の部分としてレンダリングすることもできますが、ここではそのままにしておきます...

コンテキストを別の方法で処理する必要がない場合 (セクションとカテゴリを介して記事にアクセスする) は、3 つの基本的なルート ( :articles:sections:categories) を設定してそこから移動することをお勧めします。

于 2012-04-29T14:40:08.370 に答える
0

それは非常にシンプル match 'catagories' => "catagories#index"match 'catagories/show/:id' => "catagories#show" ショーアクションです@articles = Article.where("category_id",params[:id])

@articles = Article.where("category_id",params[:id])これであなたの目的は解決します

于 2012-04-29T08:48:08.927 に答える