博客标题:Rails 中评论功能的嵌套资源实现与页面嵌入
1. 嵌套资源的配置
在开发过程中,为了实现评论与文章的关联,需要对资源进行嵌套。具体操作是编辑 config\routes.rb 文件,将以下两行代码:
map.resources :comments
map.resources :articles, :collection => { :unpublished => :get }
替换为:
map.resources :articles, :has_many => :comments, :collection => { :unpublished => :get }
或者使用等效的基于块的语法:
map.resources :articles, :collection => {:unpublished => :get} do |article|
article.resources :comments
end
添加上述代码后,运行 rake routes ,会发现所有的 /comments 路径都被替换为 /articles/:article_id/comments 。此时会拥有用于评论的 _pa
超级会员免费看
订阅专栏 解锁全文
2

被折叠的 条评论
为什么被折叠?



