0

I've created rails app and I've made dynamic error 404/500 page through ErrorsController:

class ErrorsController < ApplicationController
  def not_found
    render status: 404
  end

  def internal_server_error
    render status: 500
  end
end

and changing routes.rb to this:

Rails.application.routes.draw do
  resources :books
  root 'welcome#index'
  match "/404", :to => "errors#not_found", :via => :all
  match "/500", :to => "errors#internal_server_error", :via => :all
end

but whenever I run app this error comes:

Error during failsafe response: Missing template errors/200 with {:locale=>[:en], :formats=>[:css], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. 

and I just enable to see internal_server_error.html.erb in browser.
How can solve this?

coolmode
  • 1
  • 1
  • Not sure if this will fix your issue but you don't need `render status: 404` or `render status:500`. The rule is that if you do not explicitly render something at the end of a controller action, Rails will automatically look for the action_name.html.erb template in the controller's view path and render it. This is in accordance with "convention over configuration" principle. – Vbp Mar 19 '17 at 15:28

2 Answers2

0

Check if you have commented in you config/application.rb:

config.exceptions_app = self.routes

Also you need your templates errors/not_found.html.erb and errors/internal_server_error.html.erb

MaicolBen
  • 840
  • 1
  • 6
  • 20
  • I have created `config.exceptions_app = self.routes` and I have errors/not_found.html.erb and errors/internal_server_error.html.erb in views/errors. Does it need to create them in layouts/ folder as a template? – coolmode Mar 19 '17 at 12:19
  • No, but you need a layout for it, you can use the app layout if you want. More info: http://stackoverflow.com/questions/19103759/rails-4-custom-error-pages-for-404-500-and-where-is-the-default-500-error-mess – MaicolBen Mar 20 '17 at 03:18
  • I followed your link tutorial and my error that I mentioned in question was cleared but I don't know why I have just internal server error? Why I don't face with `not_found` error page? Actually I need `not_found` error page in production because I don't want users go to my `/books/new` page to make new book. I just want to make that page by myself in back-end. – coolmode Mar 20 '17 at 06:54
  • But do you have `match "/404"` ? or `rescue_from ActionController::RoutingError` ? Otherwise the exception won't be caught – MaicolBen Mar 20 '17 at 21:16
0

This message is saying that the page 200 (No error or OK) is not found. Your application is trying to display an error page for the status code 200...

What I would do is create a errors/200.html.erb and debug the output to see what is going on. As you said, there is an Internal server error.

Could you paste your log file (not all, just for this error)?

Jeremie
  • 2,241
  • 2
  • 17
  • 25
  • I got rid of that error but I always face with `internal_server` error. page `not_found` doesn't work. Actually I removed `errors_controller` and transferred my actions to `application_controller` and changed `routes.rb` to this: `if Rails.env.production? get '404', to: 'application#page_not_found' get '422', to: 'application#server_error' get '500', to: 'application#server_error' end` – coolmode Mar 20 '17 at 07:08