简体   繁体   English

Rails路由url名称助手

[英]Rails routing url name helpers

What basic settings are required to make sure routing url name helpers work? 确保路由URL名称助手工作需要哪些基本设置?

For instance in my route I have the following: 例如,在我的路线中,我有以下内容:

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

but the following doesn't seem to work: 但以下似乎不起作用:

new_refinery_news_url

I keep on getting the error 我继续得到错误

undefined local variable or method `new_refinery_news_url' 未定义的局部变量或方法`new_refinery_news_url'

So I'm pretty sure something is missing in the way I have configured my application, who's main routing is in the RefineryCMS gem which was added in the Gemfile. 所以我很确定我配置应用程序的方式缺少一些东西,主要路由是在Gemfile中添加的RefineryCMS gem中。

Any thoughts? 有什么想法吗?

不得不使用main_app.new_refinery_news_url

The helper name will be new_admin_news_item_url . 帮助程序名称为new_admin_news_item_url

It's simple to find all routes and their helper methods. 找到所有路线及其辅助方法很简单。 Just run rake routes and you will see: 只需运行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"}

With mountable engines you always need to specify "main_app." 使用可安装的引擎,您始终需要指定“main_app”。 (or for Refinery routes "refinery.") prefix because engines are isolated from the application. (或者对于Refinery路由“refinery。”)前缀,因为引擎与应用程序隔离。

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

Rails.application.routes.url_helpers.new_admin_news_item_path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM