【发布时间】:2016-05-03 17:49:32
【问题描述】:
嘿,我正在开发一个简单的 API,它从数据库中获取信息并将其作为 JSON 对象返回,并且它不会正常工作。我不断得到:
API::V1::BusinessesController#index 中的AbstractController::DoubleRenderError 在此操作中多次调用渲染和/或重定向。请注意,您只能调用渲染或重定向,并且每个操作最多调用一次。还要注意,redirect和render都不会终止action的执行,所以如果你想在redirect后退出一个action,你需要做一些类似“redirect_to(...) and return”的操作。
class API::V1::BusinessesController < ApplicationController
# GET /businesses
def index
@businesses = Business.all
render json: @businesses, status: :ok
paginate json: @businesses, per_page: 50
end
# GET /business/#
def show
@business = Business.find(params[:id])
render json: @business, status: :ok
end
end
另外,当我$curl -H "Content-Type: application/json" http://localhost:3000/api/v1/businesses/1 时,我只返回{"business":{"id":1}},而不是应该是{"business":{id:1, name:costco, etc}} 的整个json 对象
如果需要更多信息,请告诉我,完整的 repo 位于 github.com/dmonaco05/business-api 谢谢。
【问题讨论】:
标签: ruby-on-rails json api