【问题标题】:Custom to_json not being called in Rails在 Rails 中未调用自定义 to_json
【发布时间】:2013-04-19 06:59:35
【问题描述】:

型号

def to_json
  p 'ppp'
  p UNDISPLAYABLE_SIGNS
  if sign.in? UNDISPLAYABLE_SIGNS
    {code: code}
  else
    super
  end
end

控制器

    render json: {currency: @currency, savedInUser: true}

服务器控制台中没有打印 ppp,我该如何自定义我的 JSON 响应,我做错了什么?

【问题讨论】:

    标签: ruby-on-rails json


    【解决方案1】:

    as_json 不是 to_json

    def as_json
      p 'ppp'
      p UNDISPLAYABLE_SIGNS
      if sign.in? UNDISPLAYABLE_SIGNS
        {code: code}
      else
        super
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2011-08-03
      • 2011-06-09
      • 1970-01-01
      • 2011-10-16
      相关资源
      最近更新 更多