jbuilder

Render a JBuilder view in html view

人走茶凉 提交于 2019-12-03 11:00:36
问题 I've created a json view with JBuilder. But I want to preload this into a data object, so Backbone has access to the data early on without fetching for it. How can I render the list.json.jbuilder view into my list.html.erb view? Normally without jbuilder, I'd do something like this: <div data-list="<%= @contents.to_json %>"></div> 回答1: render , when called from within a view, returns a string rendering of the passed template or partial; you can embed that string into your view as you like.

Rabl, Jbuilder or manual json build for api?

随声附和 提交于 2019-12-03 06:50:59
To build api for a large scale application, which method is better interms of performance, should i use Rabl, Jbuilder or build json objects manually?I am building api /endpoints for mobile apps. In terms of performance, you should try creating some basic performance tests, and profile them. Assume the most complicated part of your application's model associations is your weakest point in terms of responsiveness and design your test around that. Generally speaking there are a few other things you should take into consideration. as_json overrides will quickly get out of hand in your models, and

How to send http-status using JBuilder Gem

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Am using Rails 3.0.19 and JBuilder Gem 2.0.6 to render JSON responses. JBuilder: https://github.com/rails/jbuilder Following is the code am using to send error-messages for a specific API. render :json, :template=>"/api/shared/errors.json.jbuilder", :status=> :bad_request For some reason, the client receives 200-ok status. While, I have expected 400 (bad_request). Any help, please? Here is my code in detail: def render_json_error_messages #render :template=> "/api/shared/errors.json.jbuilder", :status=> :bad_request, :formats => [:json]

Render a JBuilder view in html view

守給你的承諾、 提交于 2019-12-03 02:38:38
I've created a json view with JBuilder. But I want to preload this into a data object, so Backbone has access to the data early on without fetching for it. How can I render the list.json.jbuilder view into my list.html.erb view? Normally without jbuilder, I'd do something like this: <div data-list="<%= @contents.to_json %>"></div> render , when called from within a view, returns a string rendering of the passed template or partial; you can embed that string into your view as you like. Note though that: You have to append your template name with the type suffix/extension. If you don't, Rails

Rails4 jbuilder always return status code 200, even if I put other status code

匿名 (未验证) 提交于 2019-12-03 02:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I built a json view to return json in one of ajax call in rails4 app. I have used the idea suggested here https://stackoverflow.com/a/12832116/1560470 But I always keep getting status code as 200, even if I enforce other status code. My jbuilder view in view/managers/create.json.jbuilder looks as follows: if @manager.errors.messages.any? envelope(json, :unprocessable_entity, @manager.errors.messages) do json.success false end else envelope(json, :created) do json.success true end end My application helper lloks as follows: module

Rails JSON API layouts with Jbuilder (or other)

狂风中的少年 提交于 2019-12-03 00:11:10
In my rails 3.2 app, I'm using jbuilder to render responses from my JSON api. I want to provide a common structure to all API responses, and a layout would be the likely solution to keep my views DRY. ex: I'd like every response to be of the following form : { status: "ok|error|redirect", data: { ... JSON specific to the current view ... }, errors: [ ... ], notes: [ ... ] } (where the value for data is a json structure provided by the view, everything else is from the layout) However: I can't get the jbuilder layout yielding to the view correctly. # in layout json.data yield # in view json

HAML prevents template engines to render anything else than HTML

為{幸葍}努か 提交于 2019-12-01 04:24:43
I am using Jbuilder (and I also tried to use Rabl) to render json. When I try to render the jbuilder template in my application it renders the template within the layouts/application file and returns HTML as JSON (see line 'within layouts/application'): Rides controller on Github Started GET "/random_photo.json" Processing by RidesController#random_photo as JSON >> Rendered rides/random_photo.json.jbuilder within layouts/application (0.3ms) Rendered shared/_banners_in_corners.haml (3.0ms) Rendered shared/_sign_in_and_out.haml (2.0ms) Rendered layouts/_navigation.haml (7.3ms) Completed 200 OK

HAML prevents template engines to render anything else than HTML

▼魔方 西西 提交于 2019-12-01 02:51:09
问题 I am using Jbuilder (and I also tried to use Rabl) to render json. When I try to render the jbuilder template in my application it renders the template within the layouts/application file and returns HTML as JSON (see line 'within layouts/application'): Rides controller on Github Started GET "/random_photo.json" Processing by RidesController#random_photo as JSON >> Rendered rides/random_photo.json.jbuilder within layouts/application (0.3ms) Rendered shared/_banners_in_corners.haml (3.0ms)

Why is JBuilder not returning a response body in JSON when testing RSPEC

此生再无相见时 提交于 2019-11-30 16:54:14
When testing a JSON response from an RSPEC controller test using DHH's JBuilder, my response.body is always "{}". It works fine in development/production modes AND when I use the to_json method instead of jbuilder, I get proper JSON in my response.body. Anyone have a clue as to why my response.body would always be "{}" when testing? ----- Debugger it "should return the cart items via JSON", :focus do get :index, :format => :json end (rdb:1) response.body "{}" Kirk For anyone that is having the same issue. I have figured it out. You must call render_views within the controller tests you are

Why is JBuilder not returning a response body in JSON when testing RSPEC

限于喜欢 提交于 2019-11-30 16:23:28
问题 When testing a JSON response from an RSPEC controller test using DHH's JBuilder, my response.body is always "{}". It works fine in development/production modes AND when I use the to_json method instead of jbuilder, I get proper JSON in my response.body. Anyone have a clue as to why my response.body would always be "{}" when testing? ----- Debugger it "should return the cart items via JSON", :focus do get :index, :format => :json end (rdb:1) response.body "{}" 回答1: For anyone that is having