我使用语言代码作为前缀,例如www.mydomain.com/en/posts/1
。 这是我在routes.rb中做的:如何为url helper方法的参数设置默认值?
scope ":lang" do
resources :posts
end
现在我可以很容易地使用URL佣工如:post_path(post.id, :lang => :en)
。问题是我想将Cookie中的值用作默认语言。所以我可以只写post_path(post.id)
。
有什么办法如何设置默认值的参数在URL助手?我无法找到url helpers的源代码 - 有人能指出我正确的方向吗?
另一种方式:我已经尝试将其设置在routes.rb中,但它的启动时间只计算,这并没有为我工作:
scope ":lang", :defaults => { :lang => lambda { "en" } } do
resources :posts
end
酷,我不知道url_for被称为每次网址助手被称为!谢谢。 – 2011-06-08 17:37:53
虽然有多个url_for方法。我必须将我的url_for移动到ApplicationController并将其设置为helper_method,否则它不起作用。但无论如何,你的想法有帮助,谢谢。 – 2011-06-10 07:13:20
最上面的方法不起作用,因为你试图在新模块中不存在的方法 – 2013-05-24 06:22:58