【问题标题】:Rails - adding variable to JSON?Rails - 向 JSON 添加变量?
【发布时间】:2017-09-26 14:22:06
【问题描述】:

我正在使用这种方法从 ajax 调用中传回服装:

def givemeoutfits
  if current_user
    @outfits =  Outfit.search(params[:search],1000000,1,1000000,1,1000000,1,1000000,1).page(1).per(7).results
    @results = Outfit.search(params[:search],1000000,1,1000000,1,1000000,1,1000000,1).page(1).per(7).results.total_count
    if request.xhr?
      render status: 200, json: @outfits
  end
end 
end

我想将整数“结果”添加到 @outfits 生成的 json 中并返回给 ajax 调用。我该怎么做?

【问题讨论】:

    标签: ruby-on-rails json ajax elasticsearch


    【解决方案1】:

    您是否只想像这样在返回的 JSON 对象中包含 @results

    render status: 200, json: {
      outfits: @outfits,
      results: @results
    }
    

    然后,在您的 javascript 中,您可以与这两个键进行交互。

    【讨论】:

    • 没问题,祝你好运!如果此答案清除了所有内容,请您将其标记为已接受吗?谢谢!
    • 当然 - 我该怎么做?
    • 谢谢!如果我记得的话,您应该会在我的答案左侧看到一个复选标记,它可以让您将其标记为“已接受”。另外,欢迎使用 Stack Overflow!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2020-09-10
    相关资源
    最近更新 更多