mardi 13 décembre 2016

I am getting DoubleRenderError while using find_each

I have API method which return JSON data.
Example

 http://myapp/items/get_list_of_Item_IDs_from_some_where.json?days=50&location=CA

Method in the controller which is working:

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

Output:

[{"id":"12345","updated_at":"2016-11-18T20:31:23Z"},{"id":"12222","updated_at":"2016-11-18T20:39:18Z"}]

But when I tired to use find_each in that method.

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

Output:

I will get this error:

AbstractController::DoubleRenderError 

Aucun commentaire:

Enregistrer un commentaire