title: How to break Rails application and return 404 as JSON
body: <p>Rails application working as API service. I use mongoid </p>
  
  <p>In a controller i write now:</p>
  
  <pre><code>class UsersController &lt; ApplicationController
    def show
      @user = User.find params[:user_id]
      unless @user
        render json: { error: I18n.t('user.messages.not_found') }, status: :not_found
        return
      end
      # ... some actions with @user object 
    end
  end
  </code></pre>
  
  <p>I need make code cleaner and move this code in module. I assume that the code in controller may look like this:</p>
  
  <pre><code>class UsersController &lt; ApplicationController
    def show
      @user = User.find params[:user_id]
      not_found? @user
  
      # ... some actions with @user object
    end
  end
  </code></pre>
  
  <p>or like this</p>
  
  <pre><code>class UsersController &lt; ApplicationController
    def show
      @user = found? User, params[:user_id]
  
      # ... some actions with @user object
    end
  end
  </code></pre>
  
  <p>One of example how it can work is Pundit gem (<a href="https://github.com/elabs/pundit" rel="nofollow">https://github.com/elabs/pundit</a>). In my controller i write:</p>
  
  <pre><code>...
  def index
    authorize User, :index?
    @users = User.all
  end
  ...
  </code></pre>
  
  <p>if user hasn't access to this page then Pundit raise exception and return to client error message with 401 http code.</p>
  
  <p>What is better way to refactor my code to make controllers cleaner?</p>