Rails,通过params获取引擎控制器动作的URL

时间:2013-08-20 13:38:34

标签: ruby-on-rails

我有两个独立的引擎OfferPrices

如何从提供引擎视图获取价格引擎控制器的url,使用hash与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" }

在这种情况下,link_to引发错误:

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

我知道offers_routes.offers_path帮助器,但在我的情况下,我应该使用带有params的哈希。

1 个答案:

答案 0 :(得分:3)

如果使用引擎路线,则必须传递use_route参数。

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

来源链接:https://github.com/rails/rails/blob/v3.2.13/actionpack/lib/action_dispatch/routing/route_set.rb#L442

但这是更明确的解决方案:

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