简体   繁体   中英

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. These engines are configured a bit differently than the typical --mountable tutorial out there. In fact, they more closely resemble how Spree does it, without the Spree::ENGINE_NAME namespace.

Like Spree's architecture, I'm attempting to create core engine that my other engines depend on. For example, my backend engine's routes.rb file looks like this:

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:

<%= 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 :

<%= 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.

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. Necessary evil for this kind of work, though.

The isolate_namespace method within Core::Engine scopes everything to the spree namespace. 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. prefix on the routing helper.

If you're routing to a different engine, then you will need to have the prefix: main_app. or whatever.

The Engines Guide explains this in greater detail, and I'd recommend reading that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM