【问题标题】:How to format json in rails controller?如何在rails控制器中格式化json?
【发布时间】:2012-07-31 14:29:27
【问题描述】:

如何在 Rails 控制器中格式化 json?通过link_to我发送并进入方法params [:cat],然后我通过父(params [:cat])获取所有孩子,但是如何为我的jqTree以json格式格式化这一切?所以它看起来像 {label : (here go params[:cat]), children: [{all @search_trees.id}]} ?

require 'json'
        @search_trees = SearchTree.find(:all, :include => [:designation], :conditions => { :STR_ID_PARENT => params[:cat]})
        #data = "{ label : '10001',  children : [{label : '10111'},{label : '10122'}]  }"
        respond_to do |format|
          format.html # index.html.erb
          format.xml  { render :xml => @search_trees }
          format.json { render :json => data }
        end

我将从这里的长问题中提取部分问题:

How to correctly do jquery tree in rails app?

她是我的json:

[{"search_tree":{"STR_DES_ID":42275,"STR_ID":10130,"STR_ID_PARENT":10726,"STR_LEVEL":4,"STR_NODE_NR":130,"STR_SORT":621,"STR_TYPE": 1,"designation_id":42275}},{"search_tree":{"STR_DES_ID":42277,"STR_ID":10132,"STR_ID_PARENT":10726,"STR_LEVEL":4,"STR_NODE_NR":132,"STR_SORT": 620,"STR_TYPE":1,"designation_id":42277}},{"search_tree":{"STR_DES_ID":43152,"STR_ID":10730,"STR_ID_PARENT":10726,"STR_LEVEL":4,"STR_NODE_NR": 730,"STR_SORT":622,"STR_TYPE":1,"designation_id":43152}},{"search_tree":{"STR_DES_ID":42209,"STR_ID":12344,"STR_ID_PARENT":10726,"STR_LEVEL": 4,"STR_NODE_NR":2344,"STR_SORT":623,"STR_TYPE":1,"designation_id":42209}}] 我需要这样输出 jqTree - creating tree data from json

【问题讨论】:

    标签: jquery ruby-on-rails ruby-on-rails-3 json


    【解决方案1】:

    如果你想自定义你的 json,你可以使用 RABL 或 JBuilder。您可以在此处找到截屏视频:http://railscasts.com/episodes/320-jbuilder?autoplay=true 和此处:http://railscasts.com/episodes/322-rabl?autoplay=true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多