[英]How to render a common JS from every action of controller?
For example, I have a controller named Organizations
, which includes basic CRUD actions and all the actions respond_to
JS, so I wrote the respond_to
block at the beginning of the controller respond_to :js, { only: %i[index destroy create] }
and now want to render a common JS for all these actions ie want to render index.js.erb
on all actions rather than all action specific JS.例如,我有一个名为Organizations
的控制器,它包括基本的 CRUD 动作和所有动作respond_to
JS,所以我在控制器respond_to :js, { only: %i[index destroy create] }
的开头写了respond_to
块现在想要为所有这些动作呈现一个公共 JS,即想要在所有动作上呈现index.js.erb
,而不是所有特定于动作的 JS。 So What can be the solution for the same?那么相同的解决方案是什么?
You can override the implicit rendering by overriding the default_render
method provided by ActionController::ImplicitRender
.您可以通过覆盖ActionController::ImplicitRender
提供的default_render
方法来覆盖隐式渲染。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.