こんにちは、関連付けに少し問題があります。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 を設定します。