我在一个名为 partials
的文件夹中有我的几个部分。 ,然后我使用 render 'partials/name_of_my_partial'
将它们渲染到我的 View 中没关系。
无论如何,有没有可能以我可以使用 render 'name_of_my_partial'
的方式进行设置?和 rails 会自动检查 partials
文件夹?
现在,我有一个 Missing partial
错误。
最佳答案
除了关于这个主题的这个 SO 问题,我还没有找到其他资源,所以我在这里发布了我的努力。
在 rails 3.2+ (也在4.2中测试过),可以访问和修改lookup_context.prefixes
从 Controller Action 中。因此,要修改模板和部分前缀的查找以包含另一个路径,您可以这样做:
class MyObjectsController < ApplicationController
def show
# WARNING: Keep reeding for issues with this approach!
unless lookup_context.prefixes.first == "partials"
lookup_context.prefixes.prepend "partials"
end
end
end
这样,如果 app/views/partials/文件夹中有 show.html.erb 模板,那么它将被渲染。 show.html.erb 中引用的任何部分也是如此。
这里发生了什么?
方法
lookup_context
返回 ActionView::LookupContext
类型的对象,该对象负责保存在 ActionView
中查找模板所需的所有信息.值得注意的是,它还允许您访问 lookup_context.view_paths
,即 不是 这个问题的要求是什么,但听起来应该是。警告
修改
lookup_context.prefixes
为所有 future 的请求缓存数组。因此,为了毫无问题地使用它,最好确保我们也删除了我们添加的任何前缀。那么,有没有一种简单的方法可以做到这一点?
当然。对于我自己的项目,我创建了一个模块,我可以将其包含在任何需要此功能的 Controller 中(或仅将其包含在
ApplicationController
中)。这是我的代码:# Helper methods for adding ActionView::LookupContext prefixes on including
# controllers. The lookup_context.prefixes collection is persisted on cached
# controllers, so these helpers take care to remove the passed-in prefixes
# after calling the block.
#
# @example Expected Usage:
# around_action only: :show do |_, block|
# prepend_lookup_context_prefixes("my_optional_name_space/my_objects", &block)
# end
#
# around_action only: %i[index edit update] do |_, block|
# append_penultimate_lookup_context_prefixes("my_optional_name_space/my_objects", &block)
# end
module PrefixesHelper
# Prepends the passed in prefixes to the current `lookup_context.prefixes`
# array, calls the block, then removes the prefixes.
#
# @param [Array<String>] prefixes
def prepend_lookup_context_prefixes(*prefixes, &block)
lookup_context.prefixes.prepend(*prefixes)
block.call
remove_lookup_context_prefixes(*prefixes, index: 0)
end
# Sets the penultimate (2nd-to-last) prefixes in the current
# `lookup_context.prefixes` array, calls the block, then removes the prefixes.
#
# @param [Array<String>] prefixes
def append_penultimate_lookup_context_prefixes(*prefixes, &block)
lookup_context.prefixes.insert(-2, *prefixes)
block.call
remove_lookup_context_prefixes(*prefixes.reverse, index: -2)
end
# Removes the passed in prefixes from the current `lookup_context.prefixes`
# array. If index is passed in, then will only remove prefixes found at the
# specified index in the array.
#
# @param [Array<String>] prefixes
# @param [Integer] index
def remove_lookup_context_prefixes(*prefixes, index: nil)
prefixes.each do |prefix|
if index
if lookup_context.prefixes[index] == prefix
lookup_context.prefixes.delete_at(index)
end
else
lookup_context.prefixes.delete(prefix)
end
end
end
end
如本模块的注释中所述,本模块的预期用途是通过
around_filter
调用其中包含的方法。调用你的 Controller 。这样,模块将负责在 Controller 操作产生后删除它添加的任何前缀。例如:
around_filter only: :show do |_, block|
prepend_lookup_context_prefixes("my_optional_name_space/my_objects", &block)
end
或者:
around_filter only: %i[index edit update] do |_, block|
append_penultimate_lookup_context_prefixes("my_optional_name_space/my_objects", &block)
end
我还包括了
PrefixesHelper
模块在这里发布到一个 gem 中,我用它来向我的 Rails 应用程序添加一些不错的扩展,例如这些。如果您也想使用它,请参阅此处:https://github.com/pdobb/core_extensions
关于ruby-on-rails - 有没有办法将自定义文件夹添加到 "partials path"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7293894/