Rails Engines: Namespaced Polymorphic URLs
Asked Answered
H

3

7

I have a Rails engine, MyEngine, that doesn't have an isolated namespace. I'm trying to use the polymorphic helpers to generate links to resources, as per the docs.

An engine route:

# config/routes.rb
...
  namespace :admin do
    resources :my_resource
  end
...

Example output of rake app:routes (remember, this is an Engine):

    admin_my_resources GET    /admin/my_resources(.:format)          my_engine/admin/my_resources#index
                       POST   /admin/my_resources(.:format)          my_engine/admin/my_resources#create
 new_admin_my_resource GET    /admin/my_resources/new(.:format)      my_engine/admin/my_resources#new
edit_admin_my_resource GET    /admin/my_resources/:id/edit(.:format) my_engine/admin/my_resources#edit
     admin_my_resource PUT    /admin/my_resources/:id(.:format)      my_engine/admin/my_resources#update
                       DELETE /admin/my_resources/:id(.:format)      my_engine/admin/my_resources#destroy

If my_resource is an instance of a MyResource model with ID 12345, I'd expect:

polymorphic_url([my_engine, :admin, my_resource])

to render:

/my_engine/admin/my_resource/12345

but I was wrong. Instead, I get an exception:

undefined method `admin_my_engine_my_resource_path'...

So, polymorphic_url is trying to use admin_my_engine_my_resource_path where it really should be using something more like my_engine.admin_my_resource_path(my_resource)

Rails seems to be adding :admin the wrong way around... or am I doing it wrong?

Hirschfeld answered 29/8, 2012 at 6:23 Comment(0)
L
3

Have you tried doing this via a scope instead of a namespace?

See this SO article for a better explanation. Rails Scoped Routing

A good example of this is the devise gem.

Good luck!

Like answered 3/9, 2012 at 19:12 Comment(1)
Your answer spurred me to look closer at the namespace and scope and I've ended up creating scopes such as scope module: "admin", as: "admin" to get around it. Thanks!Hirschfeld
M
0

Run rake routes and get the url helper function name from there. You can share the rake routes output here also.

Makeyevka answered 29/8, 2012 at 6:39 Comment(2)
I need the polymorphic route helpers to work. Calling my_engine.admin_my_resource(my_resource) works nicely but the equivalent using polymorphic_url does not. I'll edit my question to be a bit clearer.Hirschfeld
Just for clarification. Does polymorphic_url([:my_engine, :admin, my_resource]) work? Just symbolizing the my_engine. I cant test this in my machine right now so asking you :)Makeyevka
S
0

I recently came across this problem in Rails 5, and found the following general solution.

Given my engine called YamsCore, which generates usual url helpers in form :

url_for(yams_core.album_tracks_path)

You can write a generic view helper accepting ANY model and generate urls via :

path = polymorphic_path([view.yams_core, my_resource])

and if you need to specify the action

path = polymorphic_path([view.yams_core, my_resource], action: :edit)

i.e.

path = polymorphic_path([view.engine_prefix, my_resource])
Sancha answered 7/3, 2019 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.