Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 活动模型序列化不使用api版本控制_Ruby On Rails_Json_Serialization - Fatal编程技术网

Ruby on rails 活动模型序列化不使用api版本控制

Ruby on rails 活动模型序列化不使用api版本控制,ruby-on-rails,json,serialization,Ruby On Rails,Json,Serialization,我有api和版本系统 我的控制器 module Api;module V1 class PlansController < ApplicationController def show @plan = Plan.find(params[:id]) render json: @plan end end end;end 然后它就可以工作了,

我有api和版本系统

我的控制器

    module Api;module V1
        class PlansController < ApplicationController        
          def show
            @plan = Plan.find(params[:id])
            render json: @plan
          end 
        end
    end;end
然后它就可以工作了,但我希望它可以工作,而不需要在每个渲染中添加序列化程序


请告诉我解决方案。

如果覆盖
渲染
,它可能会起作用

class ApplicationController < ActionController::API
  include ActionController::Serialization
  DEFAULT_SERIALIZER= V1::PlanSerializer

  def render options, &block
    options[:serializer]= DEFAULT_SERIALIZER unless options[:serializer]
    super options, &block
  end

end
class ApplicationController
根据activemodel 0.10文档-“在命名空间内序列化模型(如Api::V1::Post)时,ActiveModelSerializer将期望相应的序列化程序位于同一命名空间内(即Api::V1::PostSerializer)。”但它不起作用:(
class ApplicationController < ActionController::API
  include ActionController::Serialization
render json: @plan, serializer: V1::PlanSerializer 
class ApplicationController < ActionController::API
  include ActionController::Serialization
  DEFAULT_SERIALIZER= V1::PlanSerializer

  def render options, &block
    options[:serializer]= DEFAULT_SERIALIZER unless options[:serializer]
    super options, &block
  end

end