This is the implementation of a good tutorial on the subject (found here)

Adding this functionality is not as easy as one would think and requires some meddling around with the application controller and routes.

You can now redirect to the 404 error in your actions

def show
@post = Post.published.where(slug: params[:slug]).all.first || not_found

Remove the Rails.application.config.consider_all_requests_local condition from both the config/routes.rb and the app/controllers/application_controller.rb files if you want to test them in development.