呈现另一个命名空间的部分 [英] render partial of another namespace

查看:43
本文介绍了呈现另一个命名空间的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.我需要使用另一个命名空间的部分来渲染对象.

I have a problem. I need to render objects using partials of another namespace.

 render complain.target

它尝试从当前命名空间渲染部分(当前是 admin)

it tryes to render partial from current namespace(current is admin)

缺少部分管理员/公告/公告...

Missing partial admin/bulletins/bulletin...

我不需要从 admin/.. 渲染它

I dont need to render it from admin/..

我不能像

render partial: '/bulletins/bulletin', locals: { bulletin: complain.target }

但它是多态关联,并且使用了不同的部分路径.有什么办法吗?提前致谢!

But it's polymorphic association, and different partial pathes are used. Is it any way to do it? Thanks in advance!

推荐答案

似乎没有办法通过 render notice.target 调用(检查 Rails 5 源代码)来实现这一点.

There seems to be no possible way to achieve this with a render complain.target call (Checked on Rails 5 source).

action_view 有一个配置选项可以禁用部分的命名空间前置:

There is a config option for action_view to disable namespace prepending for partials, though:

     Rails.application.config.action_view.prefix_partial_path_with_controller_namespace = false

编辑

今天,我使用了另一种解决方案:将 Single-Table-Inheritance 模型渲染为局部模型时,可以在调用渲染局部模型时根据 Rails 的 model_name 查找传递局部变量名称:

Today, I've used another solution: When rendering Single-Table-Inheritance models into partials, one can pass the locals variable name based on the Rails model_name lookup, when calling the render partial:

<%= render partial: "admin/#{object.to_partial_path}", 
           locals: { object.model_name.element => object } 
%>

这篇关于呈现另一个命名空间的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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