ruby-on-rails - 如何从控制器的每个动作中渲染一个通用的 JS?
问题描述
例如,我有一个名为的控制器Organizations
,其中包括基本的 CRUD 动作和所有动作respond_to
JS,所以我respond_to
在控制器开头编写了块, respond_to :js, { only: %i[index destroy create] }
现在想要为所有这些动作呈现一个通用 JS,即要index.js.erb
在所有动作上呈现而不是所有特定于动作的 JS。那么同样的解决方案是什么?
解决方案
您可以通过覆盖default_render
提供的方法来覆盖隐式呈现ActionController::ImplicitRender
。
class OrganizationsController < ApplicationController
def default_render(*args)
# is there an action specific template?
if template_exists?(action_name.to_s, _prefixes, variants: request.variant)
super
# is there an index template we can use insted?
elsif template_exists?('index', _prefixes, variants: request.variant)
render :index, *args
# We don't have a template. lets just let the default renderer handle the errors
else
super
end
end
# ...
end
推荐阅读
- swift - Using two values from a function
- python - 来自两个列表的随机元素 - Python
- android - RSA 加密非法块大小异常
- ios - 如何使用选择器通过函数传递两个参数
- python - How to do a regression starting from a list of list of elements
- javascript - 与动态命名的构造函数的对象属性访问器中的括号表示法不一致。有解释吗?
- ios - Regular expression for capturing hashtags in groups
- python - Does this mechanism use the Buffer or the Cache?
- python - 获取每列中某个值的频率
- delphi - 如何将记录数组传递给java函数?