4

需要哪些基本设置来确保路由 url 名称助手工作?

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

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

4 回答 4

5

不得不main_app.new_refinery_news_url改用。

于 2012-02-05T04:14:50.613 回答
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"}
于 2011-11-30T05:05:39.650 回答
1

对于可挂载引擎,您始终需要指定“main_app”。(或炼油厂路线“炼油厂”)前缀,因为引擎与应用程序隔离。

于 2012-05-05T04:18:08.317 回答
0

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

Rails.application.routes.url_helpers.new_admin_news_item_path
于 2014-03-21T23:26:46.063 回答