相关疑难解决方法(0)

Rails:嵌套资源冲突,如何根据被调用的路由确定索引操作的范围

想象一下,你有两个定义的路线:

map.resources articles
map.resources categories, :has_many => :articles
Run Code Online (Sandbox Code Playgroud)

都可以通过助手/路径访问

articles_path # /articles
category_articles_path(1) # /category/1/articles
Run Code Online (Sandbox Code Playgroud)

如果您访问/articles,index行动从ArticlesController被执行.

如果你访问/category/1/articles,也会执行index动作ArticlesController.

那么,根据呼叫路由有条件地仅选择范围文章的最佳方法是什么?

#if coming from the nested resource route
@articles = Articles.find_by_category_id(params[:category_id])
#else
@articles = Articles.all
Run Code Online (Sandbox Code Playgroud)

routes ruby-on-rails nested-resources

12
推荐指数
1
解决办法
5308
查看次数

标签 统计

nested-resources ×1

routes ×1

ruby-on-rails ×1