覆盖渲染:json在没有模型对象的Rails 4中

我需要覆盖“渲染json”,但我不需要为模型覆盖它,在模型中我知道我可以打开ActiveRecord :: Base并重写as_json方法。 但我的问题是当我有这样的事情

class TestController < ApplicationController def index render json: { field: 'is a test' } end end 

我的问题是因为我使用了一个gem,其中一些方法我需要返回“渲染json”,而这个gem没有集中一个方法。 我认为在做py的fork并重构这个因为我可以覆盖集中式方法,但首先我想知道是否有可能覆盖渲染。

您可以打开ActionController :: Base类并在那里覆盖render方法。 但是我不确定这是否是最好的想法,这取决于你遇到的实际问题而且你没有给我们太多关于这个问题的细节,即你期望得到什么以及你实际得到了什么。

UPDATE

我可能会喜欢:

 class ActionController::Base def render *args options = args.extract_options! if options[:json] \\ do whatever you need here end args << options super *args end end