Rails Engines: Polymorphic URLs with Names

I have a Rails engine, MyEngine , which does not have an isolated namespace. I am trying to use polymorphic helpers to create resource references, according to docs .

Engine Route:

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

Example output for rake app:routes (remember, this is the 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 the my_resource model with id 12345 , I would expect:

 polymorphic_url([my_engine, :admin, my_resource]) 

for rendering:

 /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 trying to use admin_my_engine_my_resource_path where it really should use something more like my_engine.admin_my_resource_path(my_resource)

Rails seems to add :admin wrong way ... or am I doing it wrong?

+4
source share
2 answers

Have you tried to do this using a scope instead of a namespace?

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

A good example of this is a gem.

Good luck

+3
source

Run rake routes and get the function name of the sub URL. Here you can also output the output of rake routes .

0
source

All Articles