Javascript Rails结构到JSON对象

Javascript Rails结构到JSON对象,javascript,ruby-on-rails-3,json,Javascript,Ruby On Rails 3,Json,我已经基于一个简单的结构创建了一个自定义类,我需要以JSON的形式将其返回: class ItemTag < Struct.new(:id, :item_id, :type, :subtype, :top, :left, :height, :width, :name, :alternate, :photo_url, :price, :quantity, :comment, :memo) def to_map map = Hash.new

我已经基于一个简单的结构创建了一个自定义类,我需要以JSON的形式将其返回:

    class ItemTag <
      Struct.new(:id, :item_id, :type, :subtype, :top, :left, :height, :width, :name, :alternate, :photo_url, :price, :quantity, :comment, :memo)
      def to_map
        map = Hash.new
        self.members.each { |m| map[m] = self[m] }
        map
      end

      def to_json(*a)
        to_map.to_json(*a)
      end
    end
最后,在我看来,我需要将这些数据作为json字符串传递到javascript函数中,所以我需要:

<%= raw(@tags.to_json) %>
如果我将to_json放在控制器中的map操作中,它会为每个记录生成有效的json,但不会将所有对象的数组作为json提供给我


有什么想法吗?

我想这可能行得通:

def as_json(*a)
  to_map
end
ruby中的as_json方法类似于JavaScript中的to_json函数。它返回准备序列化的数据,而不是序列化的数据

[[\"1b245b45\",\"444dc0e6\",\"plant\",\"cactus\",94,661,110,174,\"Blue mistflower\",\"conoclinium coelestinum\",\" \",56.0,4,\"test\",\"test\"],[\"21e8db0c\",\"3097c392\",\"plant\",\"bamboo\",128,108,161,100,\"Green and gold\",\"chrysogonum virginianum\",\" \",76.0,5,\"this is very green\",\"dont tell them it has gold\"]]
def as_json(*a)
  to_map
end