title: Rails, development env and error pages
body: <p>I've made a simple app and I wanted to test pages for 404, 500 etc. http errors. I've changed config.consider_all_requests_local to false in my enviroments/development.rb but I've still got some problems so I would like to ask you a few questions...</p>
  
  <ol>
  <li><p>If I type in my bowser something inappropriate like <code>http://localhost:3000/products/dfgdgdgdgfd</code> I still see the old "Unknown action" site. However if I type local ip adress of my computer for ex. <code>http://192.168.1.106:3000/products/dfgdgdgdgfd</code> I can see the 404 error page from public folder. Why is that happening?</p></li>
  <li><p>I know that if I deploy my little project somewhere than my app will use the production mode and if any error would occure the 404 or 500 page will show up. But what if I want to make those error pages more dynamic ( for ex. rendering error message while using a layout with a list of popular products) or simply redirecting them to the main page?</p></li>
  </ol>
  
  <p>2.1. The first solution that I found was to use rescue_from method in application controller:</p>
  
  <pre><code>unless Rails.application.config.consider_all_requests_local
     rescue_from Exception, :with =&gt; :render_error
     rescue_from ActiveRecord::RecordNotFound, :with =&gt; :render_not_found
     rescue_from AbstractController::ActionNotFound, :with =&gt; :render_not_found
     rescue_from ActionController::RoutingError, :with =&gt; :render_not_found
     rescue_from ActionController::UnknownController, :with =&gt; :render_not_found
     rescue_from ActionController::UnknownAction, :with =&gt; :render_not_found
  end
  .
  .
  .
  private
  def render_error exception
    Rails.logger.error(exception)
    redirect_to root_path
    #or 
    # render :controller=&gt;'errors', :action=&gt;'error_500', :status=&gt;500
  end
  
  def render_not_found exception
    Rails.logger.error(exception)
    redirect_to root_path
    #or 
    # render :controller=&gt;'errors', :action=&gt;'error_404', :status=&gt;404
  end
  </code></pre>
  
  <p>... but that code didn't work at any case. </p>
  
  <p>2.2. The second solution was to place <code>match "*path" , :to =&gt; "products#show", :id=&gt;1</code> (that's the example main page in my silly app) or <code>match "*path" , :to =&gt; "errors#error_404", :id=&gt;1</code> at the end of the routes.rb file. That code works only for typos like <code>http://192.168.1.106:3000/dfgdgdgdgfd</code> because if I try <code>http://192.168.1.106:3000/products/dfgdgdgdgfd</code> (the controller exists but the action is not found) I still got the 404 page. 
  I've played a bit trying sth like <code>match "*path/*act" , :to =&gt; "products#show", :id=&gt;1</code>  or    <code>match ":controller(/*act)" , :to =&gt; "products#show", :id=&gt;8</code> but that didn't work either...</p>
  
  <p>2.3. The third solution was to make controller for errors and a file in initializers folder with this code:</p>
  
  <pre><code># initializers/error_pages.rb
  module ActionDispatch 
    class ShowExceptions 
      protected     
      def rescue_action_in_public(exception) 
         status = status_code(exception).to_s 
         template = ActionView::Base.new(["#{Rails.root}/app/views"]) 
         if ["404"].include?(status) 
           file = "/errors/404.html.erb" 
         else 
           file = "/errors/500.html.erb" 
         end         
         body = template.render(:file =&gt; file) 
         render(status, body) 
      end 
    end 
  end 
  </code></pre>
  
  <p>That was quite useful because it would let me to render dynamic erb files but.. it's not rendering any layout. I've tried to change <code>body = template.render(:file =&gt; file)</code> to <code>body = template.render(:partial =&gt; file, :layout =&gt; "layouts/application")</code> but it was only cousing errors. </p>
  
  <p>I know that I'm doing sth wrong and I belive that there is a working solution for those error pages so I hope that you can help...</p>
  
  <p>Cheers.</p>