Shalafister
Shalafister

Reputation: 399

Rails | if format does not match redirect to 404

I have a rails app and contact page. So the link is www.mypage.com/contact. When some one tries /contact.php or /contact.aspx i get 500 error. Unknown format.

Is there a way to redirect them to 404 page?

EDIT

But the problem is not the record not found. Here what I get;

An ActionView::MissingTemplate occurred in main#contact:

Missing template main/contact, application/contact with {:locale=>[:tr], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
* "/app/app/views"
* "/app/vendor/bundle/ruby/2.2.0/gems/mailboxer-0.13.0/app/views"

Upvotes: 1

Views: 768

Answers (2)

Saketram Durbha
Saketram Durbha

Reputation: 450

If you want to render a 404 error for a ActionController::UnknownFormat error, you will want to add something like this to your application_controller.rb:

rescue_from ActionController::UnknownFormat do |e|
    request.format = :html
    redirect_404
end

def redirect_404
    raise ActionController::RoutingError.new('Requested format not available.')
end

Upvotes: 0

puneet18
puneet18

Reputation: 4427

add below line to your application_controller.rb :

  rescue_from ActiveRecord::RecordNotFound do
    flash[:warning] = 'Resource not found.'
    redirect_back_or root_path
  end

Upvotes: 1

Related Questions