ole
ole

Reputation: 5233

Rails, get url to engine controller action by params

I have two isolated engines Offer and Prices.

How I can get url to Prices engine controller from Offers engine view, using hash with params?

#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 raise error:

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

I know about offers_routes.offers_path helper, but in my situation I should use hash with params.

Upvotes: 4

Views: 1175

Answers (1)

ole
ole

Reputation: 5233

You must pass the use_route param 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 more clear solution:

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

Upvotes: 3

Related Questions