Tag: error handling

Rails:恢复使用form_tag的非模型表单的内容

我的第一个Rails应用程序(使用Rails 3)取得了很好的进展。 MVC交互一切都很顺利,但是我对一个与模型没有直接关系的表单有困难。 我正在使用form_tag,并且在成功的情况下,一切都表现良好。 但是,处理错误有点不友好。 实际的错误消息存储在flash中并通过layouts / application.html正常显示,但如果表单能够记住用户刚刚填写的内容,我真的很喜欢它。但它不是:所有的字段重置为默认值。 我喜欢对对象的RESTful操作表单的方式自动记住他们提交的值,如果有错误则以红色突出显示。 没有红色突出显示我很好,但如果我能让表单的字段保持提交的值,我真的很喜欢它。 任何人都可以建议如何做到这一点? 摘录相关文件: 意见/ cardsets / import.html.erb: :import_data, :id => @cardset do %> … 1 %> … …etc (more fields) 控制器/ cardsets_controller.rb: # POST /cardsets/1/import_data def import_data success, message = @cardset.import_data(params, current_user) if success redirect_to(@cardset, :notice => message) else flash.now[:error] = message render :import end end

在Ruby on Rails中查看nil

我已经和Rails一起工作了一段时间,我发现自己经常做的一件事就是在显示之前检查我的视图代码中是否有某些属性或对象为nil。 我开始怀疑这是否总是最好的主意。 到目前为止,我的理由是,由于我的应用程序依赖于用户输入,可能会发生意外情况。 如果我从编程中学到了一件事,那就是用户输入程序员没想到的东西是运行时错误的最大来源之一。 通过检查零值,我希望回避这一点并让我的观点优雅地处理问题。 虽然我通常由于各种原因在我的模型或控制器代码中都有类似的无效或无效的值检查。 我不会在最严格的意义上称它为代码重复,但它似乎并不是很干。 如果我已经在我的控制器中检查了nil对象,那么我的视图只是假设对象真的不是零吗? 对于可以显示为零的属性,每次检查都是有意义的,但对于对象本身,我不确定什么是最佳实践。 这是我正在谈论的简化但典型的例子: 控制器代码 def show @item = Item.find_by_id(params[:id]) @folders = Folder.find(:all, :order => ‘display_order’) if @item == nil or @item.folder == nil redirect_to(root_url) and return end end 查看代码 display the item’s attributes here Oops! Looks like something went horribly wrong! 这是个好主意还是只是愚蠢?

rake任务中的Railsexception通知程序

我有一个简单的rails应用程序,带有一些控制器和一些rake任务。 由gem配置的cron执行几个任务。 我的任务之一是每天执行,有时会引发exception,默认情况下我会收到cron的警告 rake aborted! undefined method `parameterize’ for nil:NilClass Tasks: TOP => mailboxes:clean_processed (See full trace by running task with –trace) 我想调试正在发生的事情,因此我刚刚在我的Gemfile使用此行安装了此exception通知 gem gem “exception_notification”, “~> 2.4.1”, :require => ‘exception_notifier’ 并在我的application.rb文件中配置它 # enable exception notification config.middleware.use ExceptionNotifier, :email_prefix => “[MyAppName] “, :sender_address => %{“notifier” }, :exception_recipients => %w{me@example.com} 由于这个gem是一个机架中间件,它只适用于Web请求而不适用于rake任务。 我也想为rake任务启用它,我找到了这个工作的要点 。 它工作,但它不是DRY,我需要在该方法中重复gem配置,我还需要更改所有我的rake任务,将它们的语句括在一个块中,如同 exception_notify { […]

Rails中的自定义错误页面?

我需要在rails应用程序中实现一个自定义错误页面,允许我使用erb。 我一直在关注这个教程( http://blog.tommilewski.net/2009/05/custom-error-pages-in-rails/ ),我无法让它在本地(或远程)工作。 我正在运行Rails 2.3.5 这是方法的要点。 1)在’application_controller’中,我过度使用“render_optional_error_file(status_code)”方法,并将可见性设置为“protected”,就像这样。 protected def render_optional_error_file(status_code) known_codes = [“404”, “422”, “500”] status = interpret_status(status_code) if known_codes.include?(status_code) render :template => “/errors/#{status[0,3]}.html.erb”, :status => status, :layout => ‘errors.html.erb’ else render :template => “/errors/unknown.html.erb”, :status => status, :layout => ‘errors.html.erb’ end end def local_request? true end 我还在视图中创建了一个名为errors的文件夹并创建了以下视图: 404.html.erb , 422.html.erb , 500.html.erb , […]

Rails 4:如何创建使用资产管道的自定义404页面?

有许多解决方案可用于创建自定义error handling页面,但Rails 4几乎没有: 基本Rails 404错误页面 Rails中的动态错误页面 鼓励人们修改404.html in /public的标准答案对我不起作用,因为我想使用驻留在资产管道中的CSS主题。 有没有办法让html文件可以访问资产管道中定义的那些样式? 如果没有, 有没有办法创建一个可以访问管道的自定义error handling程序?

我的应用无法从服务器加载一些图像

我在我的本地WEBrick服务器上使用Ruby on Rails。 我正在从url生成一些网站图像,将它们写入我的本地文件系统并显示它们。 我通过url命名图像,但用-替换所有\ 。 有些图像似乎没有加载,因为它无法在我的文件系统上找到图像,而且我得到了损坏的图像图标。 但是,当我检查文件系统时,我发现所有图像都在那里。 这是我在日志中遇到的错误: Started GET “https://stackoverflow.com/images/image_site/http:–www.urbandictionary.com-define.php?term=slim%20shady.jpg?1309465379” for 127.0.0.1 at Thu Jun 30 13:23:06 -0700 2011 ActionController::RoutingError (No route matches “/images/image_site/http:–www.urbandictionary.com-define.php”): 这是我的HTML代码: 发生了什么,我该如何解决这个问题? 如果我需要提供更多信息,请告诉我。

使用Hash #fetch优雅地处理错误的JSON

我正在尝试从这个JSON API中获取一些产品,所以我可以在我的视图中显示它们,使用Hash#fetch #fetch优雅地处理坏JSON,如果我得到nil,则声明一个默认值。 但为什么我得到: can’t convert String into Integer 或者如果我将@json_text设置为{} : undefined method ‘fetch’ for `nil:NilClass` 直播应用: http : //runnable.com/U-QEWAnEtDZTdI_g/gracefully-handle-bad-json class MainController < ApplicationController require 'hashie' def index @json_text = <<END { "products": [] } END hashes = JSON.parse(@json_text) mashes = Hashie::Mash.new(hashes) products = [] mashes.products.fetch('products', []).each do |mash| puts "YOLO" end end end

Sinatra Logger的Web服务错误

我正在使用Sinatra 1.3 ,它是一个Sinatra::Application 。 我有一个获取Web服务的方法。 我想记录此服务何时成功以及它在后台运行时失败的原因(cron作业) def fetch_some_web_service begin #if successful log.info “Success” rescue SocketError => e log.info “Failed” end end 我似乎无法使用Sinatra logger instance 。 它为我生成错误,我假设它正在这样做,因为我正在登录一个方法,而不是在一个路径中? 使用Sinatra::Application捕获某些日志文件中的errors和success的最佳方法是什么

NameError:未定义的局部变量或方法`remember_digest和NoMethodError

我的user.rb文件包含 class User < ActiveRecord::Base attr_accessor :remember_token before_save { self.email = email.downcase } validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[az\d\-.]+\.[az]+\z/i validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } has_secure_password validates :password, presence: true, length: { minimum: 6 } # […]

为什么kernel_required.rb在我的堆栈跟踪中?

我忘了在if语句的末尾添加end这个词,并得到以下错误: /home/***/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require’: **/home/****/Desktop/ruby/food_finder/lib/restaurant.rb:84: syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)** from /home/****/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require’ from /home/****/Desktop/ruby/food_finder/lib/guide.rb:1:in `’ from /home/****/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require’ from /home/****/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require’ from init.rb:14:in `’ 我的代码没有错误: def self.saved_restaurants # read the restaurant file restaurants = [] if file_usable? file = File.new(@@filepath, ‘r’) file.each_line do |line| restaurants < forgotten end # return instances of restaurant […]