簡體   English   中英

Rails,通過params獲取引擎控制器動作的URL

[英]Rails, get url to engine controller action by params

我有兩個孤立的發動機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助手,但在我的情況下,我應該使用hash與params。

如果使用引擎路由,則必須傳遞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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM