Rendering a simple JSON array with RABL in Rails

I tried to figure out how to make RABL rendering a very simple array of JSON strings, for example: ["my,"array","of","strings"]

Controller will be

class StringsController < ApplicationController
    responds_to :json

    def index
        @string = ['my','array','of','strings']
        respond_with @strings
    end

end

And the RABL view should start with:

collection @strings, root_object: false

but I can’t understand how easy it is to output lines without the name node or inside an object ...

Obviously, there is a much simpler solution for actually servicing the JSON array, like the one I put below, but I am particularly interested in why this seems so complicated in RABL!

class StringsController < ApplicationController

    def index
        render json: ['my','array','of','strings']
    end

end
+3
source share
3 answers

To answer this question, I think it works with this:

collection @string, object_root: false

node :value do |s|
  s 
end

Note. I added :valuewhich is a JSON record label.

+2
source

Neill

, ...

index.json.rabl

collection @string, object_root: false

node do |s|
  s 
en

...

def index 
    @string = ["my","array","of","strings"]
end
0

, rabl .

def index
    @string = ['my','array','of','strings']
    respond_to do |format|
      format.json  { render :json => {:message => "Success", :my_string => @string } }
      format.xml
    end
  end
-2
source

All Articles