Server 500 internal error in missing image file (Rails 3.2.12)

If we request a dummy image file, Rails generates an internal 500-server error instead of 404. See the log below.

Here is a line in routes.rb that catches 404s:

# Catches all 404 errors and redirects
match '*url' => 'default#error_404'

Other unknown URLs are processed using 404s. What is the difference between image files and URLs with file extensions?

Started GET "/images/doesnotexistyo.png" for 71.198.44.101 at 2013-03-08 07:59:24 +0300
Processing by DefaultController#error_404 as PNG
  Parameters: {"url"=>"images/doesnotexistyo"}
Completed 500 Internal Server Error in 1ms

ActionView::MissingTemplate (Missing template default/error_404, application/error_404 with {:locale=>[:en], :formats=>[:png], :handlers=>[:erb, :builder]}. Searched in:
  * "/home/prod/Prod/app/views"
+5
source share
3 answers

The problem is that the method error_404inside the controller Defaultcannot process requests in png format. When you ask to say JSON response, you can create a URL similar to:

/controller/action.json

And inside the action you will have something like

def action
  respond_to do |format|
    format.html # Renders the default view
    format.json { render :json => @model }
    format.xml { render :xml => @model }
  end
end

, , JSON XML-, format.png, .png. :

format.png # Handle the request here...

, :)

Edit

404:

def error_404
  respond_to do |format|
    format.html
    format.png { redirect_to :controller => 'default', :action => 'error_404' }
  end
end

:)

Edit2

:

def error_404
  respond_to do |format|
    format.html { render :not_found_view }
    format.all { redirect_to controller: 'default', action: 'error_404' }
  end
end

:not_found_view 404. 404 html- self ( html-) .

, :)

+4

DefaultController? 404 Rails:

ActionController::RoutingError (No route matches [GET] "/images/doesnotexistyo.png"):

, , _404 default/error_404, , 500.

, , :

rescue_from ActiveRecord::RecordNotFound, :with => :error_404
0

Maybe not for you, but since I do some final checks for the pages dynamically in my controllers, I just follow all my 404'-servers with one to handle files other than HTML:

format.all { render :status => 404, :nothing => true }
0
source

All Articles