I am using rabl for my api calls in rails application. Here is my sample code
app.get '/item/:id' do
#errors = []
#revisions = []
request.body.rewind # in case someone already read it
#item = Entity.for_owner(params[:id], #user)
render :rabl, :item_show
end
Instead of rabl I want to use jbuilder how can I render here. I have made the view jbuilder view like this item_show.json.builder
Jbuilder.encode do |json|
json.id #item.id
json.account_id #item.account_id
end
Could any one help me how can i render this view.
I tried with #item.to_json but it is not rendering jbuilder view.
tilt-jbuilder supports Sinatra since 0.4.0, and I wrote a sample at https://gist.github.com/a2ikm/5072882 . I hope it helps you.
Change the extension of the template from .builder to .jbuilder
Related
I have a Ruby on Rails App that uses jbuilder. I am using jbuilder to help render json responses. Is there a way assign the jbuilder partial to a variable without rendering the result? I have been trying to do something like below, but I am getting this undefined method 'key' for nil:NilClass error. It seems I am not correctly passing the users model to the user.json.jbuilder Can anyone offer any help?
context = ActionController::Base.new.view_context
user_json = JbuilderTemplate.new(context) do |json|
json.partial! "/users_api/user.json.jbuilder", user: user
end.target!
You can return the JSON from Jbuilder directly with JbuilderTemplate combined with .target!, but the filename is required to start with an underscore. Starting the filename with and underscore denotes it as a partial which means it can't be rendered directly.
References (How to render Jbuidler partials inside a model?, Missing partial in rails)
Return JSON directly (calls _user.json.jbuilder)
context = ActionController::Base.new.view_context
user_json = JbuilderTemplate.new(context) do |json|
json.partial! "/users_api/user.json.jbuilder", user: user
end.target!
Render JSON (calls user.json.jbuilder which call _user.json.jbuilder)
render "/users_api/user.json.jbuilder"
user.json.jbuilder
json.partial! "/users_api/user.json.jbuilder", user: #user
_user.json.jbuilder
json.email user.email
json.name user.name
I am currently paginating the return of a query attendees that has over 9000 items. My pages and routing work fine but I would like them to appear at the bottom of the page as clickable links to that page of the results. I am relatively new at using JBuilder I am using the Kaminari gem as well as the API-Pagination gem and would like to know how to I add visible/clickable page numbers to a JBuilder view according to Kaminari Docs <%= paginate #attendees %> is all that is needed. But as far as I understand JBuilder does not work or interpret that logic as its purely manufacturing JSON objects? Any advice is appreciated as well as a better explanation of what JBuilder is doing.
Controller
module Reports
class ConferencesController < ::ApplicationController
def attendees
#conference = Conference.find(attendee_params[:conference_id])
#attendees = #conference.attendees
paginate json: #attendees, per_page: 500
end
private
def attendee_params
params.permit(:conference_id)
end
end
end
View
json.conference #conference, partial: 'conference', as: :conference
json.attendees #attendees, partial: 'attendee', as: :attendee
<%= paginate #attendees %>
Kaminari works great of the box for HTML partials, but there are some additional things you need to do to set it up for other response formats. You can remove the paginate json: #attendees, per_page: 500 line from your controller in favor of something like
#attendees = #conference.attendees.page(params[:page]).per(500)
Additionally you will need to provide additional information to your jbuilder partial to render this information.
Something like this:
json.meta do
json.total_pages #attendees.total_pages
json.total_count #attendees.total_count
end
# See the Kaminari docs for more methods available https://github.com/kaminari/kaminari#the-page-scope
Im trying to use the rabl templating system from within rails-api.
rabl looks for templates in the view path, which rails-api omits.
How would i setup rabl-rails, i have included the below lines into my Controller.
include ActionController::MimeResponds
include ActionController::ImplicitRender
respond_to :json
However:
json: #deal uses standard rails rendered.
Renderer.json(#deal) in undefined, i assume it would be available with rabl.
Any help would be appreciated.
Add:
config.view_paths = ['/app/views']
to config/initializers/rabl_init.rb. No need for rabl-rails, use plain rabl, no need for those include's, no need for respond_to.
Render with:
render 'foo/bar' # will render app/views/foo/bar.rabl template
I'm trying to figure out how to render just html with Rabl. I have an html partial without an object, so far all I can see are examples of partials with objects. If I try it without the object it just (obviously) throws an error.
The closest I got was:
node(:content) do
partial("api/v1/api/partials/tips")
end
Here is the documentation for Rabl.
UPDATE
I ended up just going with this below. Obvious right? For some reason when I tried it the first time it didn't work. So for sending only HTML in API responses, this works well.
def tips
render partial: "api/v1/api/partials/tips"
end
in a rabl view, for example show.v1.rabl you're able to render a view partial with the following (rails 4.1.2):
object #your_object
code :html do
context_scope.controller.render_to_string(
# from app/views/
partial: 'path/to/show.html.erb',
# locals (if needed)
locals: {
your_object: #your_object
}
)
end
This can be achieved by rendering an ERB template inside the Rabl node:
object #user
node(:html_content) do |user|
#user = user
template = Rails.root.to_s + '/app/views/api/users/show.html.erb'
ERB.new(File.read(template)).result(self.binding)
end
I'm building an API on Rails using ActiveRecordSerializer for serialization. When I want to render a list of resources I use:
render json: #resources
This automatically detects that there is a serializer for the resource, and uses it.
Now I want to implement pagination, and my idea is having a class called PaginatedResponse, instantiate it and render it as a json like this:
render json: PaginatedResponse.new(#resources, <more meta information of the page>)
The problem is that when I use this, everything works well but the resources are not rendered using ActiveRecordSerializer, but a default serializer. I suspect that this is happening because PaginatedResponse does not extend ActiveRecord.
Any clue how can I solve this?
Rails 4 has introduced a new concept jbuilder by default. So just create index.json.jbuilder and put the json syntex based code. Just refer the default scaffold index json jbuilder below,
json.array!(#users) do |user|
json.extract! user, :name, :email, :phone, :native_place
json.url user_url(user, format: :json)
end
This is for rendering all users with his name, phone, native_place.
So remove the line
render json: #resources
from your code and implement the the new jbuilder concept.
The solution was including ActiveModel::SerializerSupport in PaginatedResponse to indicate ActiveRecordSerializer that a serializer should be used.