Rails, get url for engine controller action by parameters

I have two isolated engines Offer and Prices .

How can I get the URL of the price movement controller in the Engine view using a hash with parameters?

 #config/routes.rb Rails.application.routes.draw do mount Offers::Engine, at: "offers", as: "offers_routes" mount Prices::Engine, at: "prices", as: "prices_routes" end #offers/offers_controller.rb class Offers::OffersController def show end end #prices/prices_controller.rb class Prices::PricesController def index end end #views/offers/show.html.slim = link_to "Prices", { action:"index", controller:"prices/prices" } 

In this case, link_to raises an error:

 *** ActionController::RoutingError Exception: No route matches {:controller=>"prices/prices"} 

I know about the offers_routes.offers_path , but in my situation I have to use a hash with parameters.

+4
source share
1 answer

You must pass the use_route parameter if you are using engine routes.

 = link_to "Prices", { action:"index", controller:"prices/prices", use_route:"prices_routes" } 

Source Link: https://github.com/rails/rails/blob/v3.2.13/actionpack/lib/action_dispatch/routing/route_set.rb#L442

But this is a clearer solution:

 = link_to "Prices", prices_routes.url_for(action:"index", controller:"prices/prices") 
+3
source

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


All Articles