there are two ways in the rails for rendering: CMIIW
first, by default, it will display a view template, example
def show end then it will display the view as usual, even you declared response_to: json, it will display the json representation, so this is why you got the MissingTemplate exception
then the following method uses render json: ... , example
class ItemsController < ApplicationController respond_to :json, :html def show @item = Item.where(params[:id]) if @item.empty? render json: { message: "Item number #{params[:id]} does not exist", status: :not_found } else render json: @item.to_json end end end
the rail guide on render really useful, you can read it here
source share