Rails 方法忽略默认参数 - 为什么?

发布于 2024-11-08 12:59:03 字数 781 浏览 0 评论 0原文

我不知道为什么会发生这种情况。我有以下功能:

def as_json(options = {})
  json = {
    :id => id,
    # ... more unimportant code
    }
  unless options[:simple]
    # ... more unimportant code
  end
  json  
end

它在大多数情况下都有效,但在我称之为的一个特定部分中:

window.JSONdata = <%= @day.to_json.html_safe %>

我收到以下错误:

ActionView::Template::Error (当您没有预料到时,您有一个 nil 对象! 您可能期望一个 Array 的实例。 评估 nil.[]) 时发生错误:

指向“unless options[:simple]”行。据我所知,选项哈希为零 - 因此该方法忽略默认参数分配。为什么?我可以通过将方法更改为来解决此问题:

def as_json(options)
  options ||= {}
  json = {
    :id => id,
    # ... more unimportant code
    }
  unless options[:simple]
    # ... more unimportant code
  end
  json  
end

这对任何人都有意义吗!?非常感谢您的帮助。

I am at a loss as to why this is happening. I have the following function:

def as_json(options = {})
  json = {
    :id => id,
    # ... more unimportant code
    }
  unless options[:simple]
    # ... more unimportant code
  end
  json  
end

It works most of the time, but in one particular partial where I call this:

window.JSONdata = <%= @day.to_json.html_safe %>

I get the following error:

ActionView::Template::Error (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]):

Pointing to the line "unless options[:simple]". As far as I can tell, the options hash is nil - thus the method is ignoring the default param assignment. WHY? I can fix this by changing the method to:

def as_json(options)
  options ||= {}
  json = {
    :id => id,
    # ... more unimportant code
    }
  unless options[:simple]
    # ... more unimportant code
  end
  json  
end

Does this make any sense to anyone!? Most appreciative for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

讽刺将军 2024-11-15 12:59:03

这是因为您使用的是 to_json,它的默认 optionsnilto_json 最终将调用 as_json 并将 nil 作为 options 传递。

这是 Rails 源代码中发生的地方。首先,to_json 是使用默认 optionsnil 定义的。

# https://github.com/rails/rails/blob/v3.0.7/activesupport/lib/active_support/core_ext/object/to_json.rb#L15
def to_json(options = nil)
  ActiveSupport::JSON.encode(self, options)
end

最终它会到达这里。

# https://github.com/rails/rails/blob/v3.0.7/activesupport/lib/active_support/json/encoding.rb#L41
def encode(value, use_options = true)
  check_for_circular_references(value) do
    jsonified = use_options ? value.as_json(options_for(value)) : value.as_json
    jsonified.encode_json(self)
  end
end

如您所见,使用 value.as_json(options_for(value)) 调用 as_json ,并且 options_for(value) 将返回默认值 < code>to_json,即 nil

This is because you're using to_json, which has a default options of nil. to_json will eventually call as_json and pass the nil as options.

Here's where it happens on the Rails source code. First, to_json is defined with the default options of nil.

# https://github.com/rails/rails/blob/v3.0.7/activesupport/lib/active_support/core_ext/object/to_json.rb#L15
def to_json(options = nil)
  ActiveSupport::JSON.encode(self, options)
end

Eventually it will arrive here.

# https://github.com/rails/rails/blob/v3.0.7/activesupport/lib/active_support/json/encoding.rb#L41
def encode(value, use_options = true)
  check_for_circular_references(value) do
    jsonified = use_options ? value.as_json(options_for(value)) : value.as_json
    jsonified.encode_json(self)
  end
end

As you see, as_json is called with value.as_json(options_for(value)) and options_for(value) will return the default value of to_json, which is nil.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文