title: Rails API - render JSON seems broken it keeps return HTML
body: <p>I am new to build Rails API from scratch (only API not web app) so I can build iOS app to connect to API. What I have an issue on my Rails API project doesn't allow me to have return JSON results, it always return HTML. Any idea what is wrong? Any suggestion appreciated. Thanks!</p>
  
  <p>What I want to have like this:</p>
  
  <pre><code>{"code":12,"title":"User doesn't exist","status":404}
  </code></pre>
  
  <p><br>
  This is what I got (it returns in red header in HTML page):</p>
  
  <pre><code>ActiveRecord::RecordNotFound in Api::V1::UsersController#show
  Couldn't find User with 'id'=5
  </code></pre>
  
  <hr>
  
  <p>Take a look at my scripts:</p>
  
  <p><strong>routes.rb</strong> (require JSON format):</p>
  
  <pre><code>Rails.application.routes.draw do
  
    namespace :api, defaults: { format: :json } do 
      namespace :v1 do
        resources :entries, except: [:new, :edit]
        resources :users, except: [:new, :edit]
      end
    end
  
  end
  </code></pre>
  
  <p><strong>../app/controllers/api/v1/users_controller.rb</strong></p>
  
  <pre><code>module Api
    module V1
      class UsersController &lt; ApplicationController
  
        before_action :set_user, only: [:show, :update, :destroy]
  
        def show
          if User.exists?(params[:id])
            @user = User.find(params[:id])
            render json: @user
          else 
            render json: { code: 12, title: "User doesn't exist", status: 404 }, status: :not_found
        end 
   .
   .
   .
  </code></pre>
  
  <h3>Note: It is very strange how this <em>show</em> method above acting weird. Part 1 is working well to return JSON results but Part 2 returns HTML instead JSON results.</h3>
  
  <p>Part 1: <code>render json: @user</code></p>
  
  <p>Part 2:  <code>render json: { code: 12, title: "User doesn't exist", status: 404 }, status: :not_found</code></p>
  
  <p><br></p>
  
  <p><strong>../app/controllers/api/v1/application_controller.rb</strong></p>
  
  <pre><code>class ApplicationController &lt; ActionController::API
  
  rescue_from ActionController::ParameterMissing, with: :render_bad_request  
  rescue_from ActiveRecord::RecordNotSaved, :with =&gt; :access_denied
  
    def render_bad_request(e)
      render json: params, status: :bad_request
    end
  
  end
  </code></pre>