Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Engines: Namespaced Polymorphic URLs

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?

like image 688
tristanm Avatar asked Aug 29 '12 06:08

tristanm


1 Answers

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 image 176
engineerDave Avatar answered Nov 06 '22 08:11

engineerDave