Ruby on rails 在Rails中使用RABL呈现简单JSON数组

Ruby on rails 在Rails中使用RABL呈现简单JSON数组,ruby-on-rails,json,rabl,Ruby On Rails,Json,Rabl,我一直在试图弄清楚如何使RABL呈现一个非常简单的JSON字符串数组,例如:[“my”,“array”,“of”,“strings”] 控制器将是 class StringsController < ApplicationController responds_to :json def index @string = ['my','array','of','strings'] respond_with @strings end e

我一直在试图弄清楚如何使RABL呈现一个非常简单的JSON字符串数组,例如:
[“my”,“array”,“of”,“strings”]

控制器将是

class StringsController < ApplicationController
    responds_to :json

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

end
但是我不知道如何在没有节点名的情况下或在对象中输出字符串

很明显,有一个更直接的解决方案来实际提供JSON数组,就像我在下面所说的,但我特别感兴趣的是为什么在RABL中这看起来如此复杂

class StringsController < ApplicationController

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

end
class StringsController
您可以这样做,而不需要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
尼尔

这应该适用于任何数组,但对于您的特定数组,确切答案是

在index.json.rabl中,使用以下代码

collection @string, object_root: false

node do |s|
  s 
en
在您的控制器中使用

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

基于Mark的答案,我认为它适用于以下内容:

collection @string, object_root: false

node :value do |s|
  s 
end

注意:我添加了JSON记录的标签
:value

在rabl中,集合将采用JSON对象而不是数组。该解决方案不会呈现简单的字符串数组,也不会回答我关于使用rabl呈现数组的问题。我希望JSON输出仅为
[“my”,“array”,“of”,“strings”]
,虽然还有其他方法,但我想知道如何在RABL中实现这一点。恐怕这对我不起作用。您给出的RABL模板生成JSON
[{},{},{}]
,而不是
['my'、'array'、'of'、'strings']
(我使用的是RABL版本0.9.3,如果这有什么区别的话)我看到您的数组在@string中,现在试试看,但不要用,只需使用render并在index.json中使用我的响应。rablI已经尝试将其设置为RABL模板上的视图规范,因此它与控制器分离,它仍然只渲染空对象而不是字符串。我注意到的一件事是,您的数组被称为[at]string,集合是[at]strings。尝试更改集合名称以匹配您正在使用的变量。您不需要仅通过渲染调用来响应。\u。呜呜。。!不幸的是,这只是我在开篇文章中的一个输入错误:)仍然不走运,不管我是否使用
#response to
,。我得到了空的对象数组。我决定改用jbuilder,这使我能够轻松地完成这项工作。看起来好像要放弃了,但我花了很多时间试图找出如何让这个简单的表示法发挥作用……有没有办法让动态键代替静态键作为值??
collection @string, object_root: false

node :value do |s|
  s 
end