user1535152
user1535152

Reputation: 266

Rails Engine partial in another engine cannot access routes of original engine

The main application is basically a container for multiple engines. One of the main engines is a "dashboard" application that will provide statistics, reports, etc. from the various other engines. When rendering partials from the other engines in the dashboard, I am unable access the routes of the original engine.

Using the Test engine as an example, I have it and the Dashboard engine mounted as follows in my main app's route.rb file:

mount Test::Engine, :at => "/test", :as => "test"
mount Dashboard::Engine, :at => "/dashboard", :as => "dashboard"

In my Test engine I have a widget in a partial that I want to use on the dashboard. In the Dashboard's view I have:

<%= render :partial => "test/dashboard/test" %>

The widget partial code shows up fine and is very basic:

<h3>Test</h3>

However if I add something trying to access one of Test's routes such as:

<%= link_to "Root", test.root_path %>

I get the following error saying that test is nil:

undefined method 'root_path' for nil:NilClass

If I move the link_to out from the widget to the Dashboard's view, it works fine. If I remove the "test" from the link_to in the widget to treat it as a regular named route in the engine, it will link to the Dashboard engine's root path; not the Test engine's.

Any ideas on how I can prevent this and access the Test engine's routes in the widget being rendered in a Dashboard view? I want to try to keep each engine's widgets modular and contained within the engine, so that way if the main application decides not to use an engine, the dashboard does not have to worry about too much extraneous code.

Upvotes: 1

Views: 703

Answers (1)

user1535152
user1535152

Reputation: 266

After much poking around, I finally figured out that the partial cannot be named the same as what the engine is mapped to in the main application. By renaming the partial to _test_widget.html.erb the NilClass error goes away since there is no conflict in the names.

Upvotes: 2

Related Questions