Can I use yield in this jbuilder? #8

Closed
hendragunz opened this Issue Dec 15, 2011 · 36 comments

Projects

None yet

9 participants

@hendragunz

I want to make layout for my API json results.
This plugin is so simple... I like that!, but can I use yield so I can make a template for my API application? How..?

Thank You, :-)

@dhh
Member
dhh commented Jan 23, 2012

Yes, you can pass the jbuilder instance around via yield and templates.

@dhh dhh closed this Jan 23, 2012
@mbrandonw

I haven't been able to get this to work. I created a layout with just this:

json.results do |json|
  yield
end

but it doesn't work. How are we supposed to do this?

@slant
slant commented May 23, 2012

I'll second this. I'm not able to yield from within a jbuilder layout either. It returns what is in the layout file, but does not inject the action-specific view.

@evgenii
evgenii commented Jun 3, 2012

Yeep I have this problem too. Here my code:

#v1.json.jbuilder - layout file
json.data yield
#index.json.jbuilder - view file
json.test 'test'

Result

{data: "{"test":"test"}"}
@freemanoid

@dhh, @evgenii how I can merge the yielded content and the content of layout?
To get smthing like this (according to the example above):

{"test":"test"}

or

#v1.json.jbuilder - layout file
json.data yield
json.this_is_layout 'ok'
#index.json.jbuilder - view file
json.this_is_template 'ok'

result

{"this_is_layout":"ok","this_is_template":"ok"}
@evgenii
evgenii commented Oct 26, 2013

You can add something like this in ./lib/jbuilder.rb file to provide this possibility (from the example in code you can see how to use it)

# Yield content
#
# Example:
#
#  layout file:
#    json.yield! JSON.parse(yield)
#    json.this_is_layout 'ok'
#  template file:
#    json.this_is_template 'ok'
#
#  {"this_is_template":"ok","this_is_layout":"ok"}
def yield!(content)
  @attributes.merge!(conntent)
end
@freemanoid

@evgenii thanks! Why is it not in mainstream?

@evgenii
evgenii commented Oct 28, 2013

I'm not a commiter here :)
plus in my case I do not need to merge yield into a root

@rwz
Collaborator
rwz commented Oct 28, 2013

There is a private method for that. I'm thinking that it might make sense to expose it though.

@elfassy
elfassy commented Oct 29, 2013

+1 please do

@freemanoid

@rwz could you show it?

@rwz rwz added a commit that referenced this issue Nov 1, 2013
@rwz rwz Expose merge! method
ref #8
0e2eb47
@rwz
Collaborator
rwz commented Nov 1, 2013
@freemanoid

@rwz if we can use it like this now?

json.merge! JSON.parse(yield)
@ghost
ghost commented Dec 6, 2013

Using Rils 3.2.16, while rendering show.json.jbuilder facing error like undefined method `dump' for MultiJson:Module.

@rwz
Collaborator
rwz commented Dec 6, 2013

@rohitbhore what versions of Jbuilder and MultiJson are you using?

@ghost
ghost commented Dec 6, 2013

@rwz jbuilder (1.5.3) and it's dependent gem multi_json (>= 1.2.0)

@rwz
Collaborator
rwz commented Dec 6, 2013

What exact version of MultiJson is that?

@ghost
ghost commented Dec 6, 2013

@rwz - I just checked out my Gemlock file i found this
jbuilder (1.5.3)
activesupport (>= 3.0.0)
multi_json (>= 1.2.0)

@ghost
ghost commented Dec 6, 2013

Fired gem list | grep multi_json and i found this
multi_json (1.8.2, 1.8.0, 1.2.0)

As you are seeing i have three versions of multi_json.

@rwz
Collaborator
rwz commented Dec 6, 2013

@rohitbhore fire console and do
MultiJson::VERSION

and

MultiJson.dump(foo: "bar")

and submit the output

@ghost
ghost commented Dec 6, 2013

I am using jbuilder gem in my application. And i dont have MultiJson module in my application.

@rwz
Collaborator
rwz commented Dec 6, 2013

You cannot install/use Jbuilder without MultiJson. It's a runtime dependency.

If you have Jbuilder and don't have MultiJson, your setup is horribly broken.

@ghost
ghost commented Dec 6, 2013

@rwz -I haven't explicitly specified multi_json gem in my Gem file. It's got installed as dependent gem and showing in Gemlock file.

@rwz
Collaborator
rwz commented Dec 6, 2013

When you add a gem to a Gemfile, all gem dependencies are added and installed as well. That is why I am asking you which version of MultiJson do you have.

Actually, that's not even so important, because Rails depend on MultiJson as well and explicitly requires it. And all versions of MultiJson have dump method defined.

Basically, you can't use Rails without MultiJson implement these methods.

Your setup is somehow broken and I recommend looking into it.

@ghost
ghost commented Dec 6, 2013

@rwz - As I have mentioned my application is currently using following multi_json versions.
'multi_json (1.8.2, 1.8.0, 1.2.0)'

And my application is running fine except when I trying to render show.json.jbuilder.

@rwz
Collaborator
rwz commented Dec 6, 2013

@rohitbhore you can't use more than one version on any gem at the same time.

@ghost
ghost commented Dec 6, 2013

@rwz - Yes we cant specify more than one versions. But gems have different dependencies and they get installed at the time of bundle install. Following is my gem list. It includes all gems from gemlist and also their dependents. And my application is running fine.

actionmailer (4.0.0.rc1, 3.2.16, 3.2.13, 3.2.8)
actionpack (4.0.1, 4.0.0.rc1, 3.2.16, 3.2.13, 3.2.8)
actionpack-action_caching (1.0.0)
actionpack-page_caching (1.0.0)
activeadmin (0.5.1)
activemodel (4.0.0.rc1, 3.2.16, 3.2.13, 3.2.8)
activerecord (4.0.0.rc1, 3.2.16, 3.2.13, 3.2.8)
activerecord-deprecated_finders (1.0.3)
activerecord-session_store (0.0.1)
activeresource (3.2.16, 3.2.13, 3.2.8)
activesupport (4.0.1, 4.0.0.rc1, 3.2.16, 3.2.13, 3.2.8)
addressable (2.3.5, 2.3.2)
airbrake (3.1.14, 3.1.6)
arbre (1.0.1)
arel (4.0.1, 3.0.3, 3.0.2)
ast (1.1.0)
atomic (1.1.14)
awesome_print (1.2.0, 1.1.0)
aws-s3 (0.6.3)
bcrypt-ruby (3.1.2, 3.0.1)
best_in_place (2.0.2, 0.2.3)
bitly (0.9.0, 0.8.1)
bourbon (3.1.8, 3.0.1)
bourne (1.1.2)
braintree (2.27.0, 2.22.0)
builder (3.1.4, 3.0.4, 3.0.3)
bundler (1.3.5)
bundler-unload (1.0.2)
capybara (2.2.0, 2.1.0, 2.0.2)
capybara-webkit (1.0.0)
carrierwave (0.6.2)
childprocess (0.3.9, 0.3.6)
chunky_png (1.2.9, 1.2.7)
client_side_validations (3.2.6, 3.2.5)
client_side_validations-formtastic (2.2.1)
coderay (1.1.0, 1.0.8)
coffee-rails (4.0.0, 3.2.2)
coffee-script (2.2.0)
coffee-script-source (1.6.3, 1.4.0, 1.3.3)
commonjs (0.2.7, 0.2.6)
commonwatir (4.0.0)
compass (0.12.2)
compass-rails (1.1.1, 1.0.3)
connection_pool (1.0.0)
covenant (0.8.0, 0.6.0)
crack (0.4.1)
cucumber (1.3.10, 1.2.1)
cucumber-rails (1.4.0, 1.3.0)
daemons (1.1.9)
database_cleaner (1.2.0, 1.0.1, 0.9.1)
delayed_job (4.0.0, 3.0.4)
delayed_job_active_record (4.0.0, 0.3.3)
devise (3.2.2, 3.0.4, 2.0.5)
diff-lcs (1.2.5, 1.1.3)
dragonfly (1.0.1, 0.9.12)
email_spec (1.5.0, 1.4.0)
erubis (2.7.0)
ethon (0.6.1)
eventmachine (1.0.3, 1.0.0)
excon (0.28.0, 0.16.10)
execjs (2.0.2, 2.0.0, 1.4.0)
executable-hooks (1.2.6)
factory_girl (4.3.0, 4.2.0, 4.1.0)
factory_girl_rails (4.3.0, 4.2.1, 4.1.0)
fake_braintree (0.4, 0.2.1)
faker (1.1.2)
faraday (0.8.8, 0.7.6)
faraday_middleware (0.9.0)
fastercsv (1.5.5)
ffi (1.9.3, 1.3.1)
file-tail (1.0.12)
fog (1.18.0, 1.8.0)
foreigner (1.6.0, 1.2.1)
formatador (0.2.4)
formtastic (2.3.0.rc2, 2.2.1)
friendly_id (4.0.10.1, 4.0.9)
fssm (0.2.10, 0.2.9)
geocoder (1.1.8, 1.1.6)
gherkin (2.12.2, 2.11.5)
girl_friday (0.11.2)
gmaps4rails (2.1.1, 1.5.6)
haml (3.1.8, 3.1.7)
haml_coffee_assets (1.16.0, 1.10.0)
has_scope (0.6.0.rc, 0.5.1)
hashie (2.0.5, 1.2.0)
hike (1.2.3, 1.2.1)
hominid (3.0.5)
httparty (0.11.0, 0.10.0)
httpauth (0.2.0)
i18n (0.6.9, 0.6.5, 0.6.1)
i18n-js (2.1.2)
inherited_resources (1.4.1, 1.3.1)
jbuilder (1.5.3)
jcrop-rails (1.0.2)
journey (1.0.4)
jquery-fileupload-rails (0.3.2)
jquery-minicolors-rails (2.1.1, 0.0.1)
jquery-rails (3.0.4, 2.1.2, 1.0.19)
jquery-timepicker-rails (1.2.5.0, 0.0.2)
jquery-ui-rails (4.1.0)
js-routes (0.9.6, 0.9.4, 0.8.5)
json (1.8.0, 1.7.7, 1.7.5)
jwt (0.1.8)
kaminari (0.15.0, 0.14.1)
kgio (2.8.1, 2.8.0)
launchy (2.4.2, 2.3.0, 2.1.2)
less (2.4.0, 2.2.2)
less-rails (2.4.2, 2.2.6)
libv8 (3.16.14.3, 3.11.8.17 x86-linux, 3.11.8.13 x86-linux)
libwebsocket (0.1.7.1)
mail (2.5.4, 2.5.3, 2.4.4)
meta_search (1.1.3)
metaclass (0.0.1)
method_source (0.8.2, 0.8.1)
mime-types (1.25.1, 1.24, 1.21, 1.19)
mini_portile (0.5.2)
minitest (4.7.5)
mocha (0.10.5)
multi_json (1.8.2, 1.7.9, 1.3.6, 1.2.0)
multi_test (0.0.2)
multi_xml (0.5.5, 0.5.2)
multipart-post (1.2.0, 1.1.5)
net-scp (1.1.2, 1.0.4)
net-ssh (2.7.0, 2.6.3)
newrelic_rpm (3.6.9.171, 3.5.5.38)
nokogiri (1.6.0, 1.5.6)
oauth2 (0.8.1, 0.6.0)
omniauth (1.1.4, 1.1.1)
omniauth-google-oauth2 (0.2.1, 0.1.13)
omniauth-oauth2 (1.1.1, 1.0.2)
orm_adapter (0.5.0, 0.0.7)
parser (2.0.0)
pg (0.17.0, 0.14.1)
phone-validator (0.0.2)
polyamorous (0.6.4, 0.5.0)
polyglot (0.3.3)
powerpack (0.0.9)
protected_attributes (1.0.2)
pry (0.9.12.4, 0.9.10)
pry-rails (0.3.2, 0.2.2)
rabl (0.8.6)
rack (1.5.2, 1.4.5, 1.4.1)
rack-cache (1.2)
rack-protection (1.5.1, 1.3.2)
rack-ssl (1.3.3, 1.3.2)
rack-test (0.6.2, 0.6.1)
rack-timeout (0.0.4)
rails (4.0.0.rc1, 3.2.16, 3.2.13, 3.2.8)
rails-observers (0.1.1)
rails3-jquery-autocomplete (1.0.10, 0.6.0)
railties (4.0.0.rc1, 3.2.16, 3.2.13, 3.2.8)
rainbow (1.1.4)
raindrops (0.12.0, 0.11.0)
rake (10.1.0, 10.0.3, 0.9.2.2)
ransack (1.1.0)
rdiscount (2.1.7, 1.6.8)
rdoc (3.12.2, 3.12)
ref (1.0.5, 1.0.4, 1.0.2)
responders (1.0.0, 0.9.3)
rest-client (1.6.7)
right_aws (3.1.0)
right_http_connection (1.4.0)
rr (1.1.2, 1.0.4)
rspec (2.12.0)
rspec-core (2.14.7, 2.12.2)
rspec-expectations (2.14.4, 2.12.1)
rspec-mocks (2.14.4, 2.12.1)
rspec-rails (2.14.0, 2.12.2)
rubinius-actor (0.0.2)
rubinius-core-api (0.0.1)
rubocop (0.15.0, 0.7.2)
ruby-hmac (0.4.0)
ruby2ruby (1.3.1)
ruby_parser (2.3.1)
rubygems-bundler (1.4.2)
rubyzip (0.9.9)
rvm (1.11.3.8)
safe_yaml (0.9.7)
sass (3.2.12, 3.2.10, 3.2.5)
sass-rails (4.0.0, 3.2.6, 3.2.5)
seed-fu (2.3.0, 2.2.0)
select2-rails (3.4.9, 3.4.3)
selenium-webdriver (2.35.1, 2.27.2)
sexp_processor (3.2.0)
shortly (0.3.3)
shoulda-matchers (2.4.0, 1.4.2)
simple-navigation (3.11.0, 3.9.0)
simple_oauth (0.1.9)
sinatra (1.4.4, 1.3.3)
slop (3.4.7, 3.3.3)
sourcify (0.6.0.rc4)
spork (1.0.0rc3)
sprockets (2.10.1, 2.2.2, 2.1.3)
sprockets-rails (2.0.1)
sqlite3 (1.3.8)
therubyracer (0.12.0, 0.11.4, 0.11.3, 0.11.2)
thin (1.6.1, 1.5.0)
thor (0.18.1, 0.17.0, 0.16.0)
thread_safe (0.1.3)
tilt (1.4.1, 1.3.6, 1.3.3)
tins (0.13.1)
treetop (1.4.15, 1.4.12, 1.4.10)
typhoeus (0.6.6, 0.4.2)
tzinfo (0.3.38, 0.3.37, 0.3.33)
uglifier (2.2.1, 2.1.2, 1.3.0)
unicorn (4.7.0, 4.6.2)
unicorn-rails (1.1.0)
uuidtools (2.1.4, 2.1.3)
valid_attribute (1.3.1)
valid_email (0.0.4)
valium (0.5.0)
warden (1.2.3, 1.1.1)
watir (5.0.0)
watir-rspec (1.1.0)
watir-webdriver (0.6.4)
webmock (1.8.7)
websocket (1.0.7, 1.0.6)
wicked (1.0.2, 0.5.0)
wicked_pdf (0.9.7, 0.9.5)
wkhtmltopdf-binary (0.9.9.1)
xml-simple (1.1.2)
xpath (2.0.0, 1.0.0)

@rwz
Collaborator
rwz commented Dec 6, 2013

@rohitbhore You're showing me the list of the gems installed in your system. This doesn't have to do anything with the gems that are loaded by the app.

@rwz
Collaborator
rwz commented Dec 6, 2013

@rohitbhore Also, the fact that your app is "working" doesn't mean anything here, except that it doesn't do much.

Every other gem out there including Rails depend on MultiJson and won't work properly without it.

Your problem is not in Jbuilder. It's in incorrect setup or some crazy code that undefines MultiJson methods.

@ghost
ghost commented Dec 6, 2013

@rwz - But when you see your applications Gemlock file then there is same gem has get installed by different Gems as their dependent with different versions.

So in my application following gems are multy_json dependent.

selenium-webdriver
sprockets
uglifier
Jbuilder

And they has installed multi_json with different versions.

@ghost
ghost commented Dec 6, 2013

@rwz - I tried the the same thing with RABL gem, and it works.
But the requirement is with Jbuilder gem only. It would be good if it works.

@freemanoid

@rohitbhore, could you push test application that repeats this error on github? I think that this is the fastest way to solve your problem.

@alex88
alex88 commented Mar 12, 2014

I'm using JSON.parse(yield), is there a way to not re-parse and re-encode the result? (just for performance reasons)

@rwz
Collaborator
rwz commented Mar 13, 2014

@alex88 could you create a separate issue for this? I have couple ideas of how to get this to work, but not if it's gonna work. An opened issue will remind me. :)

@alex88
alex88 commented Mar 13, 2014

There you go ;) #172

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment