我有一个简单的 index
我的 Controller 中提供 html 和 json 格式的操作。
def index
@records = Model.all
respond_to do |format|
format.html
format.json { render json: @records }
end
end
我最近在 JSON 端点支持的 AngularJS 中重做了 HTML View 。我从日志中注意到,在为此操作加载 HTML 时,即使我已设置 AngularJS 以在加载时在 JSON 端点获取记录,查询仍在为 HTML View 进行。
我不确定处理这个问题的最佳方法。如果我只是响应 HTML,我希望能够不查询。显而易见的方法是仅在格式为 JSON 时运行查询,但我感觉这不是最好的方法。
最佳答案
你可以简单地解决这个问题
def index
respond_to do |format|
format.html
format.json {
@records = Model.all
render json: @records
}
end
end
关于ruby-on-rails - 使用不同查询响应 html 和 json 的一个 Rails 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23592242/