[英]Rails URL and Path Helpers in Engines Like Spree
I'm trying to use some URL and/or path helpers in my Rails 4 Engine views such as resource_url or resource_path. 我正在尝试在Rails 4 Engine视图中使用一些URL和/或路径帮助程序,例如resource_url或resource_path。 These engines are configured a bit differently than the typical --mountable tutorial out there.
这些引擎的配置与那里的典型--mountable教程略有不同。 In fact, they more closely resemble how Spree does it, without the Spree::ENGINE_NAME namespace.
实际上,它们没有Spree :: ENGINE_NAME命名空间,更类似于Spree的操作方式。
Like Spree's architecture, I'm attempting to create core engine that my other engines depend on. 像Spree的体系结构一样,我正在尝试创建其他引擎所依赖的核心引擎。 For example, my backend engine's
routes.rb
file looks like this: 例如,我的后端引擎的
routes.rb
文件如下所示:
Core::Engine.add_routes do
# Check to see if the request comes in on a subdomain
# The Subdomains class passed into constraints()
# is a class defined in lib/subdomain.rb
constraints(Subdomain) do
match '/manage' => "manage#index", :via => [:get]
end
end
In a view inside my backend engine, I'd like to be able to use some URL/path helpers to do something like this: 在后端引擎内部的视图中,我希望能够使用一些URL /路径助手来执行以下操作:
<%= link_to manage_path, manage_path %>
This doesn't work, because I'm drawing the routes on the core engine. 这不起作用,因为我正在核心引擎上绘制路线。 So, I must use
所以,我必须使用
<%= link_to core_engine.manage_path, core_engine.manage_path %>
Spree somehow gets around this, but I'm not sure how. 狂欢以某种方式解决了这个问题,但我不确定如何解决。 For example, in
backend/app/views/spree/admin/products/index.html.erb
: 例如,在
backend/app/views/spree/admin/products/index.html.erb
:
<%= link_to product.try(:name), edit_admin_product_path(product) %>
Notice, the edit_admin_product_path
, but no mention of this actually being drawn on the core engine. 注意,
edit_admin_product_path
,但是没有提及实际上是在核心引擎上绘制的。
Any ideas? 有任何想法吗?
We get around this by drawing all the routes on the core engine using add_routes
which exists for reasons I won't go into here because it's a long tangent. 我们通过使用
add_routes
绘制核心引擎上的所有路由来解决这个add_routes
,存在这个原因是因为它很长的切线,所以我不add_routes
在这里讨论。 Necessary evil for this kind of work, though. 但是,这种工作必不可少。
The isolate_namespace
method within Core::Engine
scopes everything to the spree
namespace. Core::Engine
的isolate_namespace
方法将所有内容都isolate_namespace
为spree
名称空间。 If you're inside a controller that's been drawn underneath the Spree::Core::Engine
routes and you want to reference a route for another controller also drawn under that route then you can leave off the spree.
如果您位于在
Spree::Core::Engine
路线下绘制的控制器内,并且要引用也在该路线下绘制的另一个控制器的路线,则可以不使用该spree.
prefix on the routing helper. 路由助手上的前缀。
If you're routing to a different engine, then you will need to have the prefix: main_app.
如果要路由到其他引擎,则需要具有前缀:
main_app.
or whatever. 管他呢。
The Engines Guide explains this in greater detail, and I'd recommend reading that. 《 引擎指南》对此进行了更详细的解释,我建议您阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.