Tag: exception

是否可以从事务块内重新引发ActiveRecord :: Rollbackexception?

在我的rails应用程序中,我在控制器的创建操作中有一个事务块,我尝试在其中创建两个记录。 def create ActiveRecord::Base.transaction do @foo = Foo.new(params[:foo].except(:bar)) raise ActiveRecord::Rollback unless @foo.save @bar = Bar.new(params[:foo][:bar]) raise ActiveRecord::Rollback unless @bar.save end end 我希望能够从Rollback中解救,这样我就可以返回一个错误,指出哪个保存失败了。 rescue ActiveRecord::Rollback => e @foo.errors = e if @foo.errors.blank? ensure respond_with @foo end 但是,我从未进入救援区。 我假设这是因为,正如rails文档所述,事务块会占用Rollbackexception并且不会重新引发它。 有没有办法强制重新引发此exception?

设计身份validation – devise_error_messages! 在一个视图中导致nil的“未定义的方法`错误”:NilClass

devise_error_messages! 在一个视图中导致 undefined method ‘errors’ for nil:NilClass render :new后render :new create方法中的render :new 。 这是在我从“Devise :: RegistrationsController”而不是“ApplicationController”inheritanceRegistrationsController后开始发生的。 “新”方法的初始呈现不会导致任何exception。 重写注册控制器: class RegistrationsController ex flash[:alert] = ex.message render :new end end end 视图registrations / new.html.erb: resource_name, :url => registration_path(resource_name)) do |f| %> * * * * –

如何让这个动作抛出404?

我有以下行动: def show @video = @commentable = @favoritable = Video.find_by_key(params[:key]) Video.increment_counter(:views_count, @video.id) end 但是现在如果find_by_key找不到记录,它会抛出一个RuntimeError ( Called id for nil )。 那么如果找不到记录,怎么能让这个动作抛出404呢? 我正在运行Rails 3.2.1。

Rails ActiveJob – 在ActionMailer :: DeliveryJob中处理exception的好方法是什么

我在我的Rails项目中使用ActiveJob + Sidekiq进行任务处理。 我使用MyMailer.some.deliver_later直接发送邮件。 它将自动创建一个ActionMailer::DeliveryJob任务并将其放入Sidekiq队列中。 问题是,从那里处理exception有什么好处? 最好的祝福。

Hoptoad诉Exceptional诉exception_notification诉exception_logger案

以下哪种exception通知解决方案最好? 优秀 黾 exception_notification exception_logger

当用户打开多个窗口时,使用Devise在Rails中捕获401错误

场景是这样的:用户有2个窗口,他/她登录。他/她从一个登录,保持登录到另一个,然后在后者,触发一些动作,比如表单提交。 最初,发生的是它抛出了无效的真实性令牌错误。 我已经将protect_from_forgery中的protect_from_forgery更新为protect_from_forgery with: :null_session这样它就会抛出401而不是Exception。 大! 现在对于第2步,而不是用户只是看到一行文字说You need to sign in or sign up before continuing. ,我想将他/她重定向到登录页面。 这就是我遇到问题的地方……我一直在阅读本指南: http : //agileleague.com/blog/rails-3-2-custom-error-pages-the-exceptions_app-and-testing-with -capybara /表示401错误不是默认由rails捕获的。 该指南有两行代码,用于定义和捕获它,然后路由中的一行代码将使路由工作。 基本上它看起来像这样: # add to app/controllers/application_controller.rb class UnauthorizedException :unauthorized) # add to routes (in my case this is what I’ve done for Devise) devise_scope :user do match ‘/401’, to: ‘users/sessions#new’, type: “401”, […]

什么是在rails项目中保留自定义exception定义的传统位置?

制作一个自己的自定义exception时 class ThingExploded < StandardError; end class ThingIsMissing < StandardError; end 保存这些的好地方在哪里? 我正在考虑lib / exceptions.rb …并且还在考虑是否更适合以某种方式使它们更接近使用它们的代码。

当找不到id时,整个控制器的一般救援 – RoR

我偶然发现了我的应用程序查找数据库中不存在的id的情况。 抛出exception。 当然,对于任何Web开发人员来说,这都是一个非常标准的情况。 感谢这个答案,我知道使用救援处理的情况非常整齐,如下: def show @customer = Customer.find(params[:id]) rescue ActiveRecord::RecordNotFound #customer with that id cannot be found redirect_to action: :index #redirect to index page takes place instead of crashing end 如果找不到客户,则用户被重定向到索引页面。 这绝对没问题。 现在,这一切都很好,但我需要在show,edit,destroy等操作中进行相同的救援尝试,即每个需要特定id的控制器方法。 话虽如此, 这是我的问题:没有任何方法可以告诉我的控制器,如果它无法在任何方法中找到id,它将重定向到索引页面(或者,通常,执行特定的任务)?

Rails用户:您使用什么exception通知软件?

我见过Ryan Bates谈论exception_logger和exception通知。 还有其他好的东西需要考虑吗? 你喜欢和不喜欢这些什么? 另外,如果你抓住它们,这些exception通知程序会记录exception吗? 谢谢!

为什么我在Rails 4中获得连接表的未知主键例外?

这些是我的模特: class Product has_many :line_items has_many :orders, :through => :line_items end class LineItem belongs_to :order belongs_to :product end class Order has_many :line_items has_many :products, :through => :line_items end 来自schema.rb: create_table “line_items”, id: false, force: true do |t| t.integer “order_id” t.integer “product_id” t.integer “quantity” t.datetime “created_at” t.datetime “updated_at” end 我刚刚升级到Rails 4,我的连接表停止工作。 如果我执行@order.line_items ,它会抛出exception“模型LineItem中的表line_items的未知主键”。 @order.products按预期工作。 我已经尝试删除并重新创建line_items表,我已经尝试安装protected_attributes gem,但没有任何改变。 […]