Generate a nested JSON array in JBuilder
I'm not sure which attributes your product can have but i would try something like:
if @condition
json.code :success
json.array!(@branches) do |json, branch|
json.(branch, :id, :branch_name, :barcode)
json.menus branch.menus do |json,menue|
json.id menue.id
json.menu_name menue.menu_name
json.products menue.products do |json, product|
json.product_attribute_1 product.product_attribute_1
end
end
end
else
json.code :error
json.message 'Mensaje de error'
end
i'm also not quite sure why you try to nest @branches under a branch as stated by:
json.branch do
...
end
i just removed that.
Constructing a nested JSON request with Jbuilder
ARG. Ok so this code is perfectly right. The issue was that I"m using the Gon gem in conjunction with Jbuilder, and Request is a predefined class in Gon.
So just changed code to
@requestrecords.select....
And in the controller:
@requestrecords = Request.all
-__-
Using jbuilder to access child objects in has many relationship
Ok this worked:
#/test.json.builder
json.array!(@lenders) do |json, lender|
json.(lender, :id, :email, :latitude, :longitude)
json.inventories lender.inventories do |json, inventory|
json.(inventory, :id, :itemlist_id, :description)
end
end
Drawn from this answer
Need to create a json string from Jbuilder in rails Helper
Can you try with:
ApplicationController.render(template: 'demandes/index', assigns: { demandes: @demandes })
(Substitute with your template name)
Wrap single object in array for FullCalendar in Rails
You should probably split your code into three files. First create a partial from your code above, you could name it eg _trip.json.jbuilder
:
json.name trip.name
json.title trip.name
json.start_date trip.start_date
json.end_date trip.end_date
json.start trip.start_date
json.end trip.end_date
json.allDay true
if ActiveRecord::Type::Boolean.new.cast(params[:show_visits]) == true
json.visits trip.visits.each do |visit|
json.date visit.date.to_date.day.ordinalize + " " + visit.date.to_date.strftime("%b %Y")
json.park visit.park.name
json.first_on_trip visit.first_on_trip?
json.last_on_trip visit.last_on_trip?
# json.gallery visit.has_gallery? ? url_for(visit.album) : false
if visit.park.has_location?
json.park_lat visit.park.lat
json.park_lng visit.park.lng
end
end
end
I also removed instance variable and replaced it with local one. Now you can create two views where you use this partial, one for the calendar and second for the other place:
calendar.json.jbuilder
json.array! [@trip] do |trip|
json.partial! "path/to/trip", locals: { trip: trip }
end
other_place.json.jbuilder
will be even easier:
json.partial! "path/to/trip", locals: { trip: @trip }
For the exact partial rendering code please see the jBuilder documentation. It may be a bit more complicated or even easier based on location of these files in the views.
You could probably use instance variable directly in the partial, but this solution makes it a little bit more general and therefore the partial can be used in more different contexts.
Related Topics
Creating a Test Suite Using Ruby Minitest
Ubuntu 12.10 - Ruby Gem Rmagick Missing Dependency Issue
How to Destroy a Record Without an Id Column in Ruby Activerecord
Access Ruby Hash Using Dotted Path Key String
Duplicating a Ruby Array of Strings
Database Cleaner Not Working in Minitest Rails
Could Not Find Rake-10.1.0 in Any of the Sources
Best Way to Handle Category/Subcategory Relationship Ruby on Rails
How to Evaluate a Date Difference in Years, Months and Days (Ruby)
I = True and False in Ruby Is True
Converting an Array of Hashes to One Hash in Ruby
Scraping/Parsing Google Search Results in Ruby
Rails - Aciverecord Use :Dependent => :Destroy on Condition
Rails Render of Partial and Layout in Controller
Create a Human-Readable List with "And" Inserted Before the Last Element from a Ruby List
Generate a Nested JSON Array in Jbuilder