想象一下,你有两个定义的路线:
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)