假设路线
map.resources :articles
你怎么得到这个
/articles?most_popular
using link_to
method?
尝试了以下方法:
link_to articles_path(:most_popular) # exception
link_to articles_path(:most_popular => nil) # /articles
link_to articles_path(:most_popular => true) # /articles?most_popular=true
注意:我正在使用继承资源 with has_scope
如果您不向参数添加值,您将不遵守 W3C 标准,该标准要求 params 部分具有以下形式field=value
.
我建议您向文章控制器添加一个新的 :most_popular 操作。
在你的routes.rb上:
map.resources :articles, :collection => {:most_popular=>:get}
在您的控制器上:
class ArticlesController < ApplicationController
...
def most_popular
@articles = ...
end
就您的看法:
link_to most_popular_articles_path() # /articles/most_popular
这将是 HTML 兼容的,您的 url 看起来几乎相同(将 1 更改为 1 /),并且您的控制器将被简化(您将拥有与索引分离的 most_popular 操作)。
Regards!
更新(2017):W3C 标准似乎没有强制要求field=value
语法(或者不再强制要求它)。然而,一些服务器被记录为“阻塞”不符合此语法的查询。看url 查询参数没有值是否有效?了解详情。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)