开发者

Rails Path Helper for Root should output "/"

In my routes file, I have a resource called products. The index action of the products reso开发者_JAVA百科urce is also my root path.

resources :products
root :to => "products#index"

When I use the helper method products_path (in a redirect or link), it returns "/products". But what I want is for it to return "/". I know it's the same page, but I want to keep my URL's consistent.

How can I fix this?

Thanks!


root :to => 'products#index', :as => :products
match '', :to => 'products#index', :as => :root  # recreate named root_path, if you use it anywhere

This will need to appear below your resources :products as it does in your example above. This will override the products_path and products_url you'd get from resources. Run rake routes before and after to compare.


if you only want to change Index try excluding it first then defining it on its own like so:

resources :products, :except => [:index]
resources :products, :only => [:index], :path => '/'


root_path() should return /


I guess you could redefine the products_path to be the same as root_path (like in a helper file):

def products_path(*params)
  root_path *params
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜