凝望长空 发表于 2012-02-13 14:51

rails2.3.2 ExceptionNotifier 配置

rails2.3.2 ExceptionNotifier 配置








必须先在项目中要加入plugin




然后

首先在development.rb文件中加入

Ruby代码ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {   
    :address => "smtp.a-it.jp",   
    :domain => "smtp.a-it.jp",   
    :port => 25,   
    :authentication => :login,   
    :user_name => "rails",   
    :password => "rails",   
}

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
    :address => "smtp.a-it.jp",
    :domain => "smtp.a-it.jp",
    :port => 25,
    :authentication => :login,
    :user_name => "rails",
    :password => "rails",
} 然后在environment.rb中加入Ruby代码ActionView::Base.field_error_proc = Proc.new {|html_tag, instance|   
%(<span class="field-with-errors">#{html_tag}</span>)}   
#ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update( :default => '%Y-%m-%d %H:%M:%S' )   
#邮件发送地址   
ExceptionNotifier.sender_address = %("Application Error<error_sender>)   
#邮件前缀   
ExceptionNotifier.email_prefix = ""
#邮件接收者   
ExceptionNotifier.exception_recipients = %w(youremails)

ActionView::Base.field_error_proc = Proc.new {|html_tag, instance|
%(<span class="field-with-errors">#{html_tag}</span>)}
#ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update( :default => '%Y-%m-%d %H:%M:%S' )
#邮件发送地址
ExceptionNotifier.sender_address = %("Application Error<error_sender>)
#邮件前缀
ExceptionNotifier.email_prefix = ""
#邮件接收者
ExceptionNotifier.exception_recipients = %w(youremails)
再在application_controller.rb中加入Ruby代码rescue_from Exception do |exception|   
    pp exception   
    case exception   
      when "ActionController::NameError"
      flash[:error] = exception.message.to_s   
      when "ActiveRecord::RecordNotFound"
      flash[:error] = exception.message.to_s   
      when "ActionController::UnknownAction"
      flash[:error] = exception.message.to_s.split(".")rescue"No action responded to #{params[:action]}"
      else
      return rescue_action_in_public(exception)   
    end
    redirect_to :controller => 'blog', :action => "list"
end

rescue_from Exception do |exception|
    pp exception
    case exception
      when "ActionController::NameError"
      flash[:error] = exception.message.to_s
      when "ActiveRecord::RecordNotFound"
      flash[:error] = exception.message.to_s
      when "ActionController::UnknownAction"
      flash[:error] = exception.message.to_s.split(".")rescue"No action responded to #{params[:action]}"
      else
      return rescue_action_in_public(exception)
    end
    redirect_to :controller => 'blog', :action => "list"end 哈哈,这样就大功告成了!!!

第一夫人 发表于 2012-02-13 14:51

谢谢分享
页: [1]
查看完整版本: rails2.3.2 ExceptionNotifier 配置