嵌套命名空间资源、多态关联和路径

编程入门 行业动态 更新时间:2024-10-12 03:25:16
本文介绍了嵌套命名空间资源、多态关联和路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是 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.

更多推荐

嵌套命名空间资源、多态关联和路径

本文发布于:2023-08-03 14:24:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287659.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   路径   多态   资源   空间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!