Routing Rails 3.1中的路由路径

Routing Rails 3.1中的路由路径,routing,ruby-on-rails-3.1,Routing,Ruby On Rails 3.1,我在Rails 3.1中遇到以下错误: 未定义的局部变量或方法“sitemap\u home\u path”# 错误来自以下行: 看起来我的路线定义不正确。I routes.rb该路线定义如下: root :to => 'home#index' resources :home do collection do ..... get :sitemap ..... end end 因此,我希望我的url位于views/home下的sitemap.html.er

我在Rails 3.1中遇到以下错误:

未定义的局部变量或方法“sitemap\u home\u path”#

错误来自以下行:

看起来我的路线定义不正确。I routes.rb该路线定义如下:

root :to => 'home#index'

resources :home do
  collection do
    .....
    get :sitemap
    .....
  end
end
因此,我希望我的url位于views/home下的sitemap.html.erb文件中。有人能给我解释一下为什么在这种情况下没有创建sitemap\u home\u路径吗

它在以下Rails 2.1声明中运行良好:

 resources :home, :collection => {...., :sitemap => :get }

运行
rake-routes
是查看Rails为您的路由命名的好方法。在代码中,您可能会看到类似的内容:

sitemap_home_index GET    /home/sitemap(.:format)  {:action=>"sitemap", :controller=>"home"}
        home_index GET    /home(.:format)          {:action=>"index", :controller=>"home"}
                   POST   /home(.:format)          {:action=>"create", :controller=>"home"}
          new_home GET    /home/new(.:format)      {:action=>"new", :controller=>"home"}
         edit_home GET    /home/:id/edit(.:format) {:action=>"edit", :controller=>"home"}
              home GET    /home/:id(.:format)      {:action=>"show", :controller=>"home"}
                   PUT    /home/:id(.:format)      {:action=>"update", :controller=>"home"}
                   DELETE /home/:id(.:format)      {:action=>"destroy", :controller=>"home"}

因此,您希望使用

获取所需的url路径,即sitemap\u home\u路径。您的routes.rb应如下所示:

resource :home
  collection do
    get :sitemap
  end
end

原因是home是一个总是在没有ID的情况下查找的资源。因此,在这种情况下,您应该使用单个资源。由于有关路线的文章相当全面,请参阅和导轨指南。

嗨,Dylan!非常感谢你的建议。我完全忘记了那个命令。现在很清楚了。但我仍然无法将路径设置为正确的格式。你能告诉我怎么才能去掉网站地图的索引部分吗?谢谢你,雪儿!这正是我想要的。我读过那个文档,但我没有意识到除了“资源”之外还有一个“资源”关键字。