嗨,我对关联有一点问题,我正在使用 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。显示属于该类别的文章,并在文章路径的views(Category#show) 中有一个link_to。