4

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
neill
  • 90
  • 1
  • 7

3 Answers3

1

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
Mark Ellul
  • 1,906
  • 3
  • 26
  • 37
  • 1
    Afraid that this doesn't work for me. The RABL template that you have given produces the JSON `[{},{},{}]`, not `['my','array','of','strings']` (I'm using RABL version 0.9.3, if that makes any difference) – neill May 12 '14 at 13:53
  • I see that your array was in @string, try that now, but don't respond_with, just use render and use my response in index.json.rabl – Mark Ellul May 12 '14 at 16:18
  • I've tried setting this up as a view spec on the RABL template, so it is detached from the controller and it still just renders the empty objects instead of the strings. – neill May 19 '14 at 10:11
  • One thing I noticed is that your array is called [at]string and the collection is [at]strings. Try changing the collection name to match the variable you are using. You shouldn't need respond_with just a render call. – Mark Ellul May 20 '14 at 09:19
  • Woops..! Unfortunately, that's just a typo in what I've put in opening post :) Still no luck with this, it doesn't matter whether I use `#respond_to` or not,. I get the empty object array. I've decided to look at jbuilder instead, which allows me to do this easily. It seems like giving up, but I've spent a lot of time trying to find out how to get this simple representation to work.... – neill May 21 '14 at 12:26
  • Its weird because the code I have given you is exactly what I use. Good luck with jbuilder, I have never used it. Can you post the rabl template you are using? – Mark Ellul May 22 '14 at 13:49
1

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.

netwire
  • 7,108
  • 12
  • 52
  • 86
-2

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
Bharat soni
  • 2,686
  • 18
  • 27
  • That solution will not render a simple array of strings, and is also not answering my question about rendering the array with RABL. I want the JSON output to simply be `["my,"array","of","strings"]` and, although there are other approaches, I would like to know specifically how to achieve this in RABL. – neill Feb 14 '14 at 13:08