rabl

Image url in JSON (RABL, Dragonfly, Ruby on Rails)

自作多情 提交于 2019-12-10 23:47:58
问题 I'm using RABL gem, my user model has many posts users/show.rabl object @user attributes :id, :name child :posts do extends "posts/post" end posts/_post.rabl attributes :id, image: post.image.url I want to show post's image, but i have an error: undefined local variable or method "post" . But in posts/_post.html.erb i can use this <%= post.image.url =%> Images loaded by dragonfly gem. How can i get this image url in json format? 回答1: If this is a child-attribute you can append it back to the

JSON - Nesting children RABL or JBuilder for Rails

筅森魡賤 提交于 2019-12-10 19:29:47
问题 I have objects that look like: [<ltree_val: "1", contents: "blah">, <ltree_val: "1.1", contents: "blah">, <ltree_val: "1.1.1", contents: "blah">, <ltree_val: "2", contents: "blah">, <ltree_val: "2.1", contents: "blah">] Where ltree_val determines their tree structure. I need to generate something like... [{ "data" : "1", "children" : [{ "data" : "1.1", "children" : [{ "data" : "1.1.1" }] }] }, { "data" : "2" }] Where I have children which are determined by an ltree value, which are themselves

how to render a rabl view in a rake task?

喜你入骨 提交于 2019-12-10 13:08:49
问题 I'm trying to render a rabl view to string in a rails 3.2 rake task. I'm rendering it to string in order to send some JSON through Pusher from a background task. I've looked at various render_to_string from rake task answers but none of them seem to work. Here is what I have thus far: controller = PostsController.new av = ActionView::Base.new(MyApp::Application.config.paths['app/views'].first,{},controller) @post = post Pusher["some channel"].trigger('new_post', av.render(:template => 'posts

Rails api-only ignoring rabl files

扶醉桌前 提交于 2019-12-10 11:22:59
问题 I have what I believe to be a very simple app, with Rails 5 (API only) but for some reason it is returning an empty response while it should be returning a json object (using RABL) class ExercisesController < ApplicationController before_action :set_exercise, only: [:show, :update, :destroy] def index @exercises = Exercise.all # Works # render json: @exercises # Blank response render :index end end # app/views/exercises/index.json.rabl collection @exercises attributes :id, :name If I use a

Spring template engine for rendering JSON

*爱你&永不变心* 提交于 2019-12-07 14:33:52
问题 I come from Ruby and have just started testing Spring and find it fairly nice. However i'm used to being able to customize the rendered JSON output with libraries like rabl and it feels really wrong to expose internal models straight out into the JSON as i do now with the annotation @ResponseBody and just returning the model. Does anyone have any tips on libraries similar to rabl but for java/spring or is there an existing way to do it easily with spring without manually writing templates in

How can I pass an object to rabl views from rake task

巧了我就是萌 提交于 2019-12-06 12:05:00
I am trying to create a json file from a rake task using rabl. Below I have the simplified version to test with. When I view 'articles.json' or 'articles/2.json' via the url, I get the expected json response. But when I try to run it via the rake task, it always has a null value for @articles in the jsonFile created. It will render the index.json.rabl view the same number of times as @articles.count, but the values are always null. So how do I pass in the @articles object created in my rake task to Rabl.render? index.json.rabl @feedName ||= 'default' node(:rss) { partial('articles/rss),

Rails RABL respond_with error template

时间秒杀一切 提交于 2019-12-06 11:24:46
Using RABL in Rails 3.2.x, given the following controller action: respond_to :html, :json def create @foo = Foo.create(params[:foo]) respond_with @foo end Assuming the validation fails, how do you get respond_with to use a RABL template instead of the standard JSON hash of errors -- IE. I would like other model attributes besides the validation error message sent back along with the request. Suggestions? I found this one out the hard way. You should create a custom responder for your application controller, or at least your individual response. See Three reasons to love ActionController:

Rails api-only ignoring rabl files

你说的曾经没有我的故事 提交于 2019-12-06 04:42:12
I have what I believe to be a very simple app, with Rails 5 (API only) but for some reason it is returning an empty response while it should be returning a json object (using RABL) class ExercisesController < ApplicationController before_action :set_exercise, only: [:show, :update, :destroy] def index @exercises = Exercise.all # Works # render json: @exercises # Blank response render :index end end # app/views/exercises/index.json.rabl collection @exercises attributes :id, :name If I use a direct render (commented) it works properly, but with the rabl template it is blank... any thoughts? Try

Rabl, Jbuilder or manual json build for api?

折月煮酒 提交于 2019-12-04 12:10:02
问题 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. 回答1: 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

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