我喜欢 JBuilder 的语法,并且想在 Rails 之外使用它。我有一个类和一个 to_json 方法:
class Blog
def to_json
Jbuilder.encode do |json|
json.(self, :id, :logo)
json.articles articles do |json, article|
json.id article.id
end
end
end
end
不幸的是,我得到:
/Users/justin/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jbuilder-0.4.0/lib/jbuilder.rb:13:in `define_method': wrong argument type NilClass (expected Proc/Method) (TypeError)
from /Users/justin/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jbuilder-0.4.0/lib/jbuilder.rb:13:in `<class:Jbuilder>'
from /Users/justin/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/jbuilder-0.4.0/lib/jbuilder.rb:7:in `<top (required)>'
对应于:
# jbuilder.rb
class Jbuilder < BlankSlate
# Yields a builder and automatically turns the result into a JSON string
def self.encode
new._tap { |jbuilder| yield jbuilder }.target!
end
define_method(:__class__, find_hidden_method(:class)) # <--- exception
define_method(:_tap, find_hidden_method(:tap))
# ....
知道我需要在 gem 中需要哪些 gem 来满足 JBuilder 吗?