嵌套命名空间资源、多态关联和路径 [英] Nested namespaced resources, polymorphic associations and paths

查看:44
本文介绍了嵌套命名空间资源、多态关联和路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 rails 中的错误吗?我有以下路由:

Is this a bug in rails? I have the following routing:

namespace :admin do
 resources :products do
  resources :images
 end
end

创建以下路径

admin_product_image GET /admin/products/:product_id/images/:id(.:format)  admin/images#show

但是当我尝试通过

polymorphic_path( [ @imageable, @image ] ) 

它返回一个错误:未定义方法`admin_product_admin_image_path' for #<#:0x007ff8963f0f98>

It returns an error: undefined method `admin_product_admin_image_path' for #<#:0x007ff8963f0f98>

它似乎在寻找admin_product_admin_image_path"而不是admin_product_image_path"

It seems to be looking for `admin_product_admin_image_path' and not 'admin_product_image_path'

如果我调试polymorphic_routes.rb"并修改它:

If I debug "polymorphic_routes.rb" and modify this:

(proxy || self).send(named_route, *args)

正在发送admin_product_admin_image_path"并生成错误,而是发送:

which is sending "admin_product_admin_image_path" and generating the error, to rather send:

(proxy || self).send("admin_product_image_path", *args)

没有出现错误.polymorphic_path 是否不适用于命名空间?

No error is raised. Is polymorphic_path not working with namespaces?

编辑现在可以确认 polymorphic_url 函数不适用于命名空间!

Edit Can now confirm that polymorphic_url function does not work with namespaces!

编辑通过编辑坏主意 polymorphic_routes.rb,我设法硬编码了一个糟糕的主意.

Edit By editing bad idea polymorphic_routes.rb I managed to hard code a fix poor idea.

In def build_named_route_call(records, inflection, options = {}) ....
   ....
   route << model_name_from_record_or_class(record).singular_route_key
   .....
   route << model_name_from_record_or_class(record).route_key

坏*

route << model_name_from_record_or_class(record).singular_route_key.gsub(/admin_/,'').to_s

route << model_name_from_record_or_class(record).route_key.gsub(/admin_/,'').to_s

推荐答案

这是 rails 中的一个错误.我上面问题中的编辑是修复需要去的地方......但我的硬编码编辑破坏了轨道中的其他东西 - 所以避免它.

This is a bug in rails. The edit in my question above is where the fix needs to go ... but my hard coded edit breaks other things in rails - so avoid it.

目前唯一的解决方案是不在 Rails 中使用命名空间、嵌套资源和多态关联.

The only solution at the moment is to not used namespaced, nested resources and polymorphic associations in rails.

这篇关于嵌套命名空间资源、多态关联和路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆