简体   繁体   中英

Rails routing url name helpers

What basic settings are required to make sure routing url name helpers work?

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'

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.

Any thoughts?

不得不使用main_app.new_refinery_news_url

The helper name will be new_admin_news_item_url .

It's simple to find all routes and their helper methods. Just run rake routes and you will see:

          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." (or for Refinery routes "refinery.") prefix because engines are isolated from the application.

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

Rails.application.routes.url_helpers.new_admin_news_item_path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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