as_json 中的参数数量错误 [`timeout` 方法被保留!] [英] Wrong number of arguments in as_json [`timeout` method is reserved!]

查看:39
本文介绍了as_json 中的参数数量错误 [`timeout` 方法被保留!]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rails 中遇到与我的模型之一相关的错误,我们将其称为 Unit.以下代码产生错误:

I get an error in Rails that is related to one of my models, let's call it Unit. The following code produces an error:

format.json { render :json => @units.as_json }

错误是关于参数数量错误(0 of 1).

The error is about wrong number of parameters (0 of 1).

推荐答案

我解决了这个问题.来自 Rails 源:

I solved the problem. From the Rails source:

  module Serialization
    def serializable_hash(options = nil)
      options ||= {}

      attribute_names = attributes.keys.sort
      if only = options[:only]
        attribute_names &= Array.wrap(only).map(&:to_s)
      elsif except = options[:except]
        attribute_names -= Array.wrap(except).map(&:to_s)
      end

      hash = {}
      attribute_names.each { |n| hash[n] = read_attribute_for_serialization(n) } # exception here

      # ...
    end

    alias :read_attribute_for_serialization :send

    # ...
  end

  # ...
end

所以真正的错误是调用例如返回的方法之一Unit.first.attributes.keys.sort (["dev_id", "flags", "id", "inote", "ip", "location_id", "model_id", "name", "period", "phone", "port", "snote", "timeout"]) 期望我向它传递一个参数.这个方法是timeout,它是Rails 猴子补丁对象的一些私有方法.所以问题的正确解决方案是将这个属性重命名为别的东西.

So the real error is that one of the methods returned by calling e.g. Unit.first.attributes.keys.sort (["dev_id", "flags", "id", "inote", "ip", "location_id", "model_id", "name", "period", "phone", "port", "snote", "timeout"]) expects me to pass an argument to it. This method is timeout, it is some private method Rails monkey patches Object with. So the correct solution to the problem is to rename this attribute to something else.

这篇关于as_json 中的参数数量错误 [`timeout` 方法被保留!]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆