Mickey Sheu
Mickey Sheu

Reputation: 768

How do I explicitly set a rails engine routing proxy?

On my Rails 5.0.7 application I have Thredded (0.15.4) mounted as an engine in a namespace.

#routes.rb

namespace :home do
  mount Thredded::Engine => "/communities", as: "thredded"
end

This allows me to access /home/communities to pull up the thredded interface.

Unfortunately, this also leads to unexpected behavior. The rails route proxy is not thredded as expected, but rather home_thredded. If I wanted to access a named route inside my engine, I need to do home_thredded.root_path instead of thredded.root_path

This is especially troublesome because thredded.root_path is actually referenced in the code of the thredded gem itself, which now throws an error.

Is there some way to explicitly set the routing proxy to avoid needing to include the namespace in the routing proxy?

A few things that I've considered and rejected:

  1. I could directly edit the code in the offending file of the gem, but that would be annoying to maintain.

  2. I could also write a decorator that overwrites the offending method so that it uses the correct rails routing proxy, but that would still mean that I would have to use the home_thredded.some_path anywhere rather than the expected thredded.some_path

  3. I could not namespace the engine. This could work, but it would break the naming conventions of the rest of the application.

Upvotes: 0

Views: 388

Answers (1)

jvillian
jvillian

Reputation: 20263

Have you tried:

scope :home do
  mount Thredded::Engine => "/communities", as: "thredded"
end

That should add /home to your URI Pattern but leave your Controller#Action and Prefixs unchanged.

Upvotes: 2

Related Questions