3
votes

I've been trying to figure out how to make RABL render a very simple JSON array of string, such as: ["my,"array","of","strings"]

The controller would be

class StringsController < ApplicationController
    responds_to :json

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

end

And the RABL view must surely start with:

collection @strings, root_object: false

but I cannot figure out how to just output the strings without a node name or within an object...

There is obviously a much more straightforward solution to actually serving up the JSON array, much like what I've put below, but I'm specifically interested in why this seems so complicated in RABL!

class StringsController < ApplicationController

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

end
3
In rabl the collection will take json object not array. - Bharat soni

3 Answers

1
votes

Neill,

This should work for any arrays, but for your particular array the exact answer is...

In the index.json.rabl use the below code

collection @string, object_root: false

node do |s|
  s 
en

In your controller use...

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

To build on top of Mark's answer, I think it works with this:

collection @string, object_root: false

node :value do |s|
  s 
end

Note I added :value which is the label of the JSON record.

-2
votes

You can do it like that and that need not the rabl view for showing response.

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