2

JBuilderの構文が大好きで、レールの外で使用したいと思います。私はクラスと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))
  # .... 

JBuilderを満たすためにgemに必要なgemはありますか?

4

1 に答える 1