0

私は Rails を初めて使用し、ブログ サイトを作成しています。投稿にカテゴリが必要で、最終結果の URL は次のようになります - foo.com/category(title)/post(title) なのでたくさんのグーグルの後、このレールキャストhttp://railscasts.com/episodes/47-two-many-to-manyを見ました。次のモデルがありますが、ルートがどうあるべきかわかりません。

すべての投稿を表示するには、カテゴリのインデックス ビューを使用する必要がありますか?

モデル - 分類

class Categorisations < ActiveRecord::Base
  attr_accessible :category_id, :product_id
end

モデル - カテゴリ

class Category < ActiveRecord::Base
  attr_accessible :title, :image
  has_many :categorisations
  has_many :posts, :through => :categorisations
end

モデル - ポスト

class Post < ActiveRecord::Base
attr_accessible :body, :is_verified, :publish_date, :title, :url, :tag_list, :image, :category_id
  has_many :categorisations
  has_many :categories, :through => :categorisations
end
4

1 に答える 1

3
resources :categories do
  resources :posts
end

/category/1/post/1 のリソースを提供します

http://guides.rubyonrails.org/routing.html#nested-resources

ulrのタイトルについては、このRailscast をご覧ください。

編集:

以前のコメントに答えるには、これをお勧めします: @category = Category.find(params[:id]) @posts = @category.posts

于 2013-08-21T21:23:41.980 に答える