0
我創建基於一個簡單的結構自定義類,我需要把它找回來的JSON:Rails的結構,以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
在我的控制,我收集像這樣這樣的數據:
@tags = design.item_links.all.map{ |pi| ItemTag.new(pi.id,pi.item_id,pi.item.type,pi.item.subtype,pi.top,pi.left,pi.height,pi.width,pi.item.name,pi.item.alternate_name,pi.item.logo_photo(:thumb),pi.price,pi.quantity,pi.public_note,pi.private_note)}
終於,在我看來,我需要這個數據作爲JSON字符串傳遞到JavaScript函數,所以我做的:
<%= raw(@tags.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\"]]
如果我把to_json控制器我的地圖行動裏面,它會爲每個記錄有效的JSON,但它不會給我的陣列所有的對象都是JSON。
任何想法?