title: Returning a 403 when a 404 occurs in Rails API
body: <p>I have a Rails API with a controller that has a <code>before_action</code> on the create function that checks if the current_user creating the "paste" is also the owner of the specified <code>room_id</code> in <code>paste_params</code>. </p>
  
  <p>If <code>current_user.rooms.find(params[:paste][:room_id]).user_id</code> isn't found or if it is found but isn't equal to <code>current_user.id</code> the server always returns a 404.</p>
  
  <p>How can I go about making it return a 403 instead of a 404? Because this check is meant to determine whether the user creating the paste is also the owner of the room the paste will be linked to, if the user isn't the owner of that room it means they aren't authorized to create the paste under that room.</p>
  
  <p>Here's the relevant parts of the controller:</p>
  
  <pre><code>class Api::V1::PastesController &lt; ApplicationController
    before_action :check_room_owner, only: %i[create update destroy]
  
    def create
      paste = current_user.pastes.build(paste_params)
      if paste.save
        render json: PasteSerializer.new(paste).serializable_hash, status: :created
      else
        render json: { errors: paste.errors }, status: :unprocessable_entity
      end
    end
  
    private
  
    # Convert to 403 forbidden if not found
    def check_room_owner
      head :forbidden unless current_user.rooms.find(params[:paste][:room_id]).user_id === current_user.id
    end
  
  end
  </code></pre>
  
  <p>Cheers!</p>