Rails routing url name helpers

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

For example, on my way, 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 does not work:

 new_refinery_news_url 

I keep getting an error

undefined local variable or method `new_refinery_news_url '

So, I'm pretty sure that something is missing in how I set up my application, the main routing of which is in the RefineryCMS stone, which was added to the Gemfile.

Any thoughts?

+4
source share
4 answers

Had to use main_app.new_refinery_news_url .

+5
source

The helper name will be new_admin_news_item_url .

Just 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"} 
+2
source

With mounted engines, you always need to specify "main_app". (or for routes of refinery refineries.) because the engines are isolated from the application.

+1
source

The solution, if you use routes outside the refinery, is to prefix named_path with a Rails object that contains methods for named routes.

 Rails.application.routes.url_helpers.new_admin_news_item_path 
0
source

Source: https://habr.com/ru/post/1383792/


All Articles