Catch Unknown action in Rails 3 for custom 404

前端 未结 4 1811
挽巷
挽巷 2021-01-31 12:14

I want to catch unknown action error in Rails 3, that shows \"Unknown Action\" error on development and the 404.html on production. I tried putting this rescue_from

相关标签:
4条回答
  • 2021-01-31 12:34

    Using a catch all route to handle 404 erros (as @Seth Jackson suggested) has one major drawback: if you use any Rails engines that define their own routes (such as Jammit) their routes will be ignored.

    Better and more compliant solution would be to use a Rack middleware that will catch 404 errors. In one of my projects, I've implemented such Rack middleware that reports these errors to Hoptoad. I've based my implementation on this one: https://github.com/vidibus/vidibus-routing_error, but instead of invoking my Rails app again to handle the 404 error, I do it in the Rack middleware and let nginx to show the 404 page.

    0 讨论(0)
  • 2021-01-31 12:40

    If you really want to rescue AbstractController::ActionNotFound in a controller, you can try something like this:

    class UsersController < ApplicationController
    
      private
    
      def process(action, *args)
        super
      rescue AbstractController::ActionNotFound
        respond_to do |format|
          format.html { render :404, status: :not_found }
          format.all { render nothing: true, status: :not_found }
        end
      end
    
    
      public
    
      # actions must not be private
    
    end
    

    This overrides the process method of AbstractController::Base that raises AbstractController::ActionNotFound (see source).

    0 讨论(0)
  • 2021-01-31 12:44

    Have you tried a catch all route?

    http://railscasts.com/episodes/46-catch-all-route

    The wildcard route that you are current using is a Bad Idea(tm).

    I would recommend defining the routes you care about, and then doing a catchall as the last line of routes.rb (routes defined first in routes.rb trump later definitions). Then you can render whatever page you want (and specify the 404 status code).

    Edit: If you really want to use your current approach... (although this seems like it could be deprecated)

    def rescue_action(exception) case exception when ActionNotFound, UnknownAction then # Handle these exceptions here else super end end

    0 讨论(0)
  • 2021-01-31 12:47

    rescue_from was slightly broken when Rails 3 came out (still broken in 3.1 too). Basically you can't:

    rescue_from ActionController::RoutingError
    

    anymore. See here.

    The solution, for now, is what hamiltop recommends. Use a catch all route that goes to your "routing error" route. Make sure you put it at the end of your config\routes.rb file so it is processed last.

    # Any routes that aren't defined above here go to the 404
    match "*a", :to => "application#routing_error"
    
    def routing_error
        render "404", :status => 404
    end
    

    Note: This method has one major drawback. If you use an engine such as Jammit or Devise the catch all will route will make Rails ignore the engine's routes.

    If you aren't using an engine that has it's own routes then you should be fine. However, if you do use an engine that defines its own routes see @arikfr's answer.

    0 讨论(0)
提交回复
热议问题