RAILS 4 to_json包括多个vars

时间:2015-12-28 15:05:57

标签: ruby json ajax ruby-on-rails-4

我可以使用 protected override void Request(Request request, IObserver<Response> observer) { query.Arrive += p => QueryReceive(request.RequestId, p, observer, query); query.Error += (type, s, message) => QueryError(observer, message); query.NoMoreData += id => QueryCompleted(observer); query.Execute(request); } private void QueryError(IObserver<PositionSheetResponse> observer, string message) { observer.OnError(new Exception(message)); } private void QueryCompleted(IObserver<PositionSheetResponse> observer) { observer.OnCompleted(); } private void QueryReceive(Guid requestId, Qry0079Receive receiveData, IObserver<PositionSheetResponse> observer, IQry0079PositionSheet query) { observer.OnNext(ConvertToResponse(requestId, receiveData)); } :include对json-response进行to_json ass:

def stats
  @orders = Order.all
  respond_to do |format|
    format.json { render :json => @orders.to_json(:include => :review) }
  end
end

它运作正常,但如果我需要多个变量的关联怎么办?

此:

def stats
  @orders = Order.all
  @tasks = Task.all
  respond_to do |format|
    format.json { render :json => {
        orders: @orders.to_json(:include => :review),
        tasks: @tasks.to_json(:include => :user)
      }
    }
  end
end

无效 - 它返回一个字符串而不是json: enter image description here

2 个答案:

答案 0 :(得分:0)

你可以试试这个。请参阅以下示例

<强> e.g。

ActiveSupport::JSON.decode(orders)

将Json字符串解码为Hash。

答案 1 :(得分:0)

这是jbuilder开始的情况,它是Rails 4中默认的ruby gem