新手:Ruby rails。使用 find_each 时出现 DoubleRenderError
Newbie: Ruby on rails. I am getting DoubleRenderError while using find_each
我有 API 方法 return JSON 数据。
范例
http://myapp/items/get_list_of_Item_IDs_from_some_where.json?days=50&location=CA
控制器中正在运行的方法:
def get_list_of_Item_IDs_from_some_where
item = Item.where("created_at >= ? and location = ?", Date.today - params[:days].to_i, params[:location])
serialized_item_ids_and_updated_at = item.as_json(only: [:id, :updated_at])
respond_to do |format|
format.html
format.json { render json: serialized_item_ids_and_updated_at }
end
end
输出:
[{"id":"12345","updated_at":"2016-11-18T20:31:23Z"},{"id":"12222","updated_at":"2016-11-18T20:39:18Z"}]
当我厌倦了在该方法中使用 find_each 时控制器中的方法不起作用。
def get_list_of_Item_IDs_from_some_where
Item.where("created_at >= ? and location = ?", Date.today - params[:days].to_i, params[:location]).find_each do |item|
serialized_item_ids_and_updated_at = item.as_json(only: [:id, :updated_at])
respond_to do |format|
format.html
format.json { render json: serialized_item_ids_and_updated_at }
end
end
end
输出:
我会得到这个错误:
AbstractController::DoubleRenderError
每个请求只允许渲染一次,当您在块中使用渲染执行循环时,它将渲染与集合中的总项目一样多。
这是我的解决方案:
def get_list_of_Item_IDs_from_some_where
jsons = Item.where("created_at >= ? and location = ?", Date.today - params[:days].to_i, params[:location]).to_json(only: [:id, :updated_at])
render json: jsons
end
这对我有用。
def get_list_of_Item_IDs_from_some_where
serialized_item_ids_and_updated_at = []
Item.where("created_at >= ? and location = ?", Date.today - params[:days].to_i, params[:location]).find_each do |item|
serialized_item_ids_and_updated_at << item.as_json(only: [:id, :updated_at])
end
respond_to do |format|
format.html
format.json { render json: serialized_item_ids_and_updated_at }
end
end
我有 API 方法 return JSON 数据。
范例
http://myapp/items/get_list_of_Item_IDs_from_some_where.json?days=50&location=CA
控制器中正在运行的方法:
def get_list_of_Item_IDs_from_some_where
item = Item.where("created_at >= ? and location = ?", Date.today - params[:days].to_i, params[:location])
serialized_item_ids_and_updated_at = item.as_json(only: [:id, :updated_at])
respond_to do |format|
format.html
format.json { render json: serialized_item_ids_and_updated_at }
end
end
输出:
[{"id":"12345","updated_at":"2016-11-18T20:31:23Z"},{"id":"12222","updated_at":"2016-11-18T20:39:18Z"}]
当我厌倦了在该方法中使用 find_each 时控制器中的方法不起作用。
def get_list_of_Item_IDs_from_some_where
Item.where("created_at >= ? and location = ?", Date.today - params[:days].to_i, params[:location]).find_each do |item|
serialized_item_ids_and_updated_at = item.as_json(only: [:id, :updated_at])
respond_to do |format|
format.html
format.json { render json: serialized_item_ids_and_updated_at }
end
end
end
输出:
我会得到这个错误:
AbstractController::DoubleRenderError
每个请求只允许渲染一次,当您在块中使用渲染执行循环时,它将渲染与集合中的总项目一样多。
这是我的解决方案:
def get_list_of_Item_IDs_from_some_where
jsons = Item.where("created_at >= ? and location = ?", Date.today - params[:days].to_i, params[:location]).to_json(only: [:id, :updated_at])
render json: jsons
end
这对我有用。
def get_list_of_Item_IDs_from_some_where
serialized_item_ids_and_updated_at = []
Item.where("created_at >= ? and location = ?", Date.today - params[:days].to_i, params[:location]).find_each do |item|
serialized_item_ids_and_updated_at << item.as_json(only: [:id, :updated_at])
end
respond_to do |format|
format.html
format.json { render json: serialized_item_ids_and_updated_at }
end
end