How to Redirect to a 404 in Rails

How to redirect to a 404 in Rails?

Don't render 404 yourself, there's no reason to; Rails has this functionality built in already. If you want to show a 404 page, create a render_404 method (or not_found as I called it) in ApplicationController like this:

def not_found
raise ActionController::RoutingError.new('Not Found')
end

Rails also handles AbstractController::ActionNotFound, and ActiveRecord::RecordNotFound the same way.

This does two things better:

1) It uses Rails' built in rescue_from handler to render the 404 page, and
2) it interrupts the execution of your code, letting you do nice things like:

  user = User.find_by_email(params[:email]) or not_found
user.do_something!

without having to write ugly conditional statements.

As a bonus, it's also super easy to handle in tests. For example, in an rspec integration test:

# RSpec 1

lambda {
visit '/something/you/want/to/404'
}.should raise_error(ActionController::RoutingError)

# RSpec 2+

expect {
get '/something/you/want/to/404'
}.to raise_error(ActionController::RoutingError)

And minitest:

assert_raises(ActionController::RoutingError) do 
get '/something/you/want/to/404'
end

OR refer more info from Rails render 404 not found from a controller action

How do I redirect to 404 page when a user record does not exist?

Try this code instead:

def show
@user = User.find_by(id: params[:id])

raise ActionController::RoutingError.new('Not Found') if @user.blank?
end

Redirect 404, 422, 500 Ruby On rails

In your route file:

#routes.rb
get '*unmatched_route', to: 'application#raise_not_found'

In your application controller

#application_controller.rb
rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from Exception, with: :not_found
rescue_from ActionController::RoutingError, with: :not_found

def raise_not_found
raise ActionController::RoutingError.new("No route matches #{params[:unmatched_route]}")
end

def not_found
respond_to do |format|
format.html { render file: "#{Rails.root}/public/404", layout: false, status: :not_found }
format.xml { head :not_found }
format.any { head :not_found }
end
end

def error
respond_to do |format|
format.html { render file: "#{Rails.root}/public/500", layout: false, status: :error }
format.xml { head :not_found }
format.any { head :not_found }
end
end

You can find this a complete resource here

How redirect to 404 page in routes.rb?

If you want custom error pages, you'll be best looking at this answer I wrote a few weeks ago


You need several important elements to create custom error routes:

-> Add custom error handler in application.rb:

# File: config/application.rb
config.exceptions_app = self.routes

-> Create /404 routes in your routes.rb:

# File: config/routes.rb
if Rails.env.production?
get '404', :to => 'application#page_not_found'
end

-> Add actions to application controller to handle these routes

# File: app/controllers/application_controller.rb
def page_not_found
respond_to do |format|
format.html { render template: 'errors/not_found_error', layout: 'layouts/application', status: 404 }
format.all { render nothing: true, status: 404 }
end
end

This is obviously relatively basic, but hopefully it will give you some more ideas on what you can do

How do I redirect to 404 page when any records do not exist?

try this :)

    class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found

private

def record_not_found
render file: "#{Rails.root}/public/404", layout: true, status: :not_found
end
end

How to redirect an entire site to a 404 page in rails 3

In Rails you can create a method to render 404's in the Application Controller, ActiveRecord and Abstract Controller:

def make_404
raise ActionController::RoutingError.new('404')
end


Related Topics



Leave a reply



Submit