Rails路由url名称助手

时间:2011-11-30 04:55:58

标签: ruby-on-rails routing refinerycms

确保路由网址名称帮助有效,需要哪些基本设置?

例如,在我的路线中,我有以下内容:

Blog::Application.routes.draw do
  resources :news, :as => :news_items, :controller => :news_items, :only => [:show, :index]

  scope :module => "refinery" do
    scope(:path => 'refinery', :as => 'admin', :module => 'Admin') do
      resources :news, :except => :show, :as => :news_items, :controller => :news_items
    end
  end
end

但以下似乎不起作用:

new_refinery_news_url

我一直在收到错误

  

未定义的局部变量或方法`new_refinery_news_url'

所以我很确定我配置应用程序的方式缺少一些东西,主要路由是在Gemfile中添加的RefineryCMS gem中。

有什么想法吗?

4 个答案:

答案 0 :(得分:5)

不得不改为使用main_app.new_refinery_news_url

答案 1 :(得分:2)

帮助者名称为new_admin_news_item_url

找到所有路线及其辅助方法很简单。只需运行rake routes,您就会看到:

          news_items GET    /news(.:format)                   {:action=>"index", :controller=>"news_items"}
           news_item GET    /news/:id(.:format)               {:action=>"show", :controller=>"news_items"}
    admin_news_items GET    /refinery/news(.:format)          {:action=>"index", :controller=>"refinery/Admin/news_items"}
                     POST   /refinery/news(.:format)          {:action=>"create", :controller=>"refinery/Admin/news_items"}
 new_admin_news_item GET    /refinery/news/new(.:format)      {:action=>"new", :controller=>"refinery/Admin/news_items"}
edit_admin_news_item GET    /refinery/news/:id/edit(.:format) {:action=>"edit", :controller=>"refinery/Admin/news_items"}
     admin_news_item PUT    /refinery/news/:id(.:format)      {:action=>"update", :controller=>"refinery/Admin/news_items"}
                     DELETE /refinery/news/:id(.:format)      {:action=>"destroy", :controller=>"refinery/Admin/news_items"}

答案 2 :(得分:1)

使用可安装引擎,您始终需要指定“main_app”。 (或者对于Refinery路由“refinery。”)前缀,因为引擎与应用程序隔离。

答案 3 :(得分:0)

如果您使用炼油厂以外的路线,解决方案是在named_pa​​th前面加上包含命名路线方法的Rails对象

Rails.application.routes.url_helpers.new_admin_news_item_path