Динамические страницы ошибок в Rails 3 - PullRequest
38 голосов
/ 16 марта 2011

В Rails 2.3.x вы можете переопределить render_optional_error_file примерно так:

# ApplicationController.rb
protected
  def render_optional_error_file(status_code)
    render :template => "errors/500", :status => 500, :layout => 'application'
  end

Однако в Rails 3 больше нет render_optional_error_file. Вместо этого вам нужно переопределить rescue_action_in_public, что вы можете сделать так:

# config/initializers/error_page.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 => file)

        render(status, body)
      end

  end
end

Это работает, но не использует макет приложения. Однако, если вы укажете путь макета следующим образом:

body = template.render(:file => file, :layout => "layouts/application") # line 15

Вы получаете Error during failsafe response: ActionView::Template::Error.

Строка 4 файла application.html.erb: 4:

<%= stylesheet_link_tag "app", "jquery-ui", "jquery.fancybox", :cache => "all" %>

Все, что ActionView обычно использует для визуализации шаблонов, не загружается.

Трассировка стека:

  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:794:in `join'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:794:in `rails_asset_id'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:817:in `rewrite_asset_path'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:746:in `compute_public_path'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:424:in `path_to_stylesheet'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:875:in `ensure_stylesheet_sources!'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:874:in `each'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:874:in `ensure_stylesheet_sources!'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:512:in `stylesheet_link_tag'
  /data/sites/fundraisers-stage/releases/20110316194843/app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___19482063_70294907435920_0'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/template.rb:135:in `send'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/template.rb:135:in `render'
  /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/notifications.rb:54:in `instrument'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/template.rb:127:in `render'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/render/layouts.rb:80:in `_render_layout'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/render/rendering.rb:62:in `_render_template'
  /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/notifications.rb:52:in `instrument'
  /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/notifications.rb:52:in `instrument'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/render/rendering.rb:56:in `_render_template'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/render/rendering.rb:26:in `render'
  /data/sites/fundraisers-stage/releases/20110316194843/config/initializers/error_pages.rb:15:in `rescue_action_in_public'

Ответы [ 4 ]

55 голосов
/ 15 июня 2012

В rails 3.2 это проще, чем это:

Добавьте это к config/application.rb:

config.exceptions_app = self.routes

, что приводит к тому, что ошибки маршрутизируются через маршрутизатор.Затем вы просто добавляете к config/routes.rb:

match "/404", :to => "errors#not_found"

Я получил эту информацию из пункта № 3 в сообщении в блоге " Мои пять любимых скрытых функций в Rails 3.2 ", автор - Хосе Валим.

51 голосов
/ 19 марта 2011

Я бы предложил использовать rescue_from . Вы бы просто спаслись от конкретных ошибок, а не переопределяли rescue_action_in_public. Это особенно полезно при работе с пользовательскими ошибками или ошибками, характерными для контроллера.

# ApplicationController
rescue_from ActionController::RoutingError, :with => :render_404
rescue_from ActionController::UnknownAction, :with => :render_404
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
rescue_from MyApp::CustomError, :with => :custom_error_resolution

def render_404
  if /(jpe?g|png|gif)/i === request.path
    render :text => "404 Not Found", :status => 404
  else
    render :template => "shared/404", :layout => 'application', :status => 404
  end
end

# UsersController
rescue_from MyApp::SomeReallySpecificUserError, :with => :user_controller_resolution

http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html

3 голосов
/ 20 марта 2011

В уведомлении об исключении есть метод с именем notify_about_exception для инициирования уведомления об ошибке по запросу.

class ApplicationController < ActionController::Base
  include ExceptionNotification::Notifiable

  rescue_from Exception, :with => :render_all_errors

  def render_all_errors(e)
    log_error(e) # log the error
    notify_about_exception(e) # send the error notification

    # now handle the page
    if e.is_a?(ActionController::RoutingError)
      render_404(e)
    else
      render_other_error(e)
    end
  end

  def render_404_error(e)
   # your code
  end

  def render_other_error(e)
   # your code
  end
end
0 голосов
/ 03 августа 2014

Я тоже сталкивался с такой проблемой.Проблема из-за гема или плагина attachment_fu.Просто удалите его и используйте любой другой плагин или гем, который решит вашу проблему.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...