Rails Engines: polymorphic URLs with names

微信扫一扫,分享到朋友圈

Rails Engines: polymorphic URLs with names

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?

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!

微信扫一扫,分享到朋友圈

Rails Engines: polymorphic URLs with names

Briefing: Tencent partner opens new offline digital retail site in Beijing

上一篇

测试工程师怎么甩锅!

下一篇

你也可能喜欢

Rails Engines: polymorphic URLs with names

长按储存图像,分享给朋友