title: Rails 6, standalone Jbuilder in a service : partials not loading
body: <p>I thought I could use Jbuilder outside of Rails controllers. The documentation said so.</p>
  
  <p>I wrote this module, to include into a service object. Which is integrated in a Rails 6 api app, but is not inheriting from ActiveRecord.</p>
  
  <pre><code>module JsonGenerator
    def to_json
      template = File.read("#{::Rails.root}/path/to/template.json.jbuilder")
      Jbuilder.new { |json| eval template }.target!
    end
  end
  </code></pre>
  
  <pre><code>class Converter::Album
    include JsonGenerator
  end
  </code></pre>
  
  <p>This is working if I don't use any partial. But when I do, using a syntax like this:</p>
  
  <pre><code>json.artists artists do |artist|
    json.partial! 'artist', artist: artist
  end
  </code></pre>
  
  <p>I get this error :</p>
  
  <pre><code>Failure/Error: eval template
  
  TypeError:
  {:artist=&gt;#&lt;Converter::Artist:0x00007fd0d1a99dc8 @name="Bob"&gt;} is not a symbol nor a string
  </code></pre>
  
  <p>The problem seems to be that Jbuilder partials can't accept PORO as argument ?</p>
  
  <p>I don't want to use <code>eval</code> anyway, but it's the only way I found for loading a template which is not in the <code>views</code> folder.</p>
  
  <p>If I copy paste all my template directly as an argument to the <code>Jbuilder.new</code> method, I get the same error.</p>
  
  <p><strong>Update 04/12/2019</strong></p>
  
  <p>I tried this line </p>
  
  <pre><code>json.partial! partial: 'artist', locals: { artist: artist }
  </code></pre>
  
  <p>and even though it loads the partial, the data it generates is wrong.</p>
  
  <p>I get this</p>
  
  <pre><code>"artists" =&gt; [{"partial!"=&gt;{"locals"=&gt;{"name"=&gt;"Bob", "instrument"=&gt;"piano"}, "partial"=&gt;"artist"}}]
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>"artists" =&gt; [{"name"=&gt;"Bob", "instrument"=&gt;"piano"}]
  </code></pre>