草庐IT

fnon-call-exceptions

全部标签

ruby-on-rails - Rails 和 attr_accessible : is there a way to raise an exception if a non-mass-assignable attribute is mass-assigned?

如果尝试批量分配attr_accessible不允许的属性,是否有办法让Rails引发错误?这在开发中会很方便,可以提醒我为什么我Shiny的新模型不起作用,也有助于登录生产环境以检测恶意事件。我正在使用Rails2.3.8,但可能很快就会迁移到3。 最佳答案 从Rails3.2开始,这不再需要monkeypatching——rails现在提供了这种行为。将其放入development.rb和test.rb:config.active_record.mass_assignment_sanitizer=:strict

ruby-on-rails - 弃用警告 : Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s)

我将我的Rails5.1.4应用更新到了5.2.0。我的一个模型中有以下范围:scope:by_category,lambda{|category_slug|category_ids=Category.find_by(slug:category_slug)&.subtree_idswhere(category_id:category_ids)}由于该范围,Rails返回以下错误:DEPRECATIONWARNING:Dangerousquerymethod(methodwhoseargumentsareusedasrawSQL)calledwithnon-attributeargume

ruby-on-rails - Ruby无一异常(exception)地获得回溯

我有一个RubyonRails应用程序,其中一个模型的验证失败。对于此验证可能失败的地方,代码库有不同的入口点。我有兴趣弄清楚它是从哪里来的。由于这是一个简单的验证方法,因此不涉及任何异常,我只是从方法中返回false并且保存失败。目前是否还可以记录回溯以查明此验证源自哪个服务/路由,以便我可以查看是什么导致此对象的状态发生更改以使其验证失败? 最佳答案 你可以试试caller():deffoo2putscallerenddeffoofoo2#line5endfoo#line7结果:test.rb:5:in`foo'test.rb:

ruby - 这是什么 & :last Ruby construct called?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Whatdoesmap(&:name)meaninRuby?survey.map(&:questions).flatten.compact之类的东西叫什么,所以我可以找到有关它们的更多信息:)。&:解决了什么问题,或者它到底在做什么?它是否用于其他语言?

ruby - Rspec any_instance.stub 为 nil :NilClass exception 引发未定义的方法 `any_instance_recorder_for'

这是我正在测试的包含在Foo.rb中的类:classFoodefbarreturn2endend这是Foo_spec.rb中包含的我的测试:require"./Foo.rb"describe"Foo"dobefore(:all)doputs"#{Foo==nil}"Foo.any_instance.stub(:bar).and_return(1)endit"shouldpassthis"dof=Foo.newf.bar.shouldeq1endend我得到以下输出:falseFFailures:1)FooShouldpassthisFailure/Error:Foo.any_insta

ruby - 公寓 ruby gem : Want to Catch an exception

我正在使用这个apartmentruby。我在application.rb文件中添加了这个:config.middleware.use'Apartment::Elevators::Subdomain'当我尝试在PostgreSQL中不存在子域“test”模式的浏览器url“test.domain.local:3000”中点击这个时,我看到了这个错误Apartment::SchemaNotFound(Oneofthefollowingschema(s)isinvalid:test,"public")我知道这是gem的正常行为,但想捕获此异常并将用户重定向到其他页面,我该怎么做?

ruby-on-rails - rails : link_to calls custom method in controller

我希望使用link_to来调用我的Controller中的方法。但是,由于某些奇怪的原因,路由会寻找show方法。在我看来:..beverage.id)%>..在我的config/routes.rb中match'beverages/archive'=>'beverages#archive'在我的beverages_controller.rb中defarchivebeverage=Beverage.find(params[:id])respond_todo|format|#format.html#show.html.erbformat.json{renderjson:beverage}e

ruby - 如何通过正则表达式查找括号内的文本,但有一些异常(exception)?

我有一个正则表达式/^\[(text:\s*.+?\s*)\]/mi目前可以捕获以text开头的括号中的文本:。以下是它的工作示例:[text:hereismytextthatiscapturedwithinthebrackets.]现在,我想添加一个异常(exception),以便它允许某些括号,如下例所示:[text:hereismytextthatiscapturedwithinthebracketsandalsoinclude![](/some/path)]基本上,我需要它允许匹配中的![](/some/path)括号。如有任何帮助,我们将不胜感激。谢谢。更新:下面是括号内的文

ruby-on-rails - rails 3 : Call functions inside controllers

如果我想在Controller内部调用函数,我应该把它们放在哪里? 最佳答案 如果您希望它在Controller本地,那么您需要做的就是将它添加到您希望使用的Controller。privatedefmyfunctionfunctioncode.....end对于所有Controller,您可以将它放在应用程序Controller中,因为所有Controller都是子类。应用程序Controllerprotecteddefmyfunctionfunctioncode.....end如果你想访问你的View,那么你可以创建一个助手应用

ruby-on-rails - 之前(:each) for all tests except one

这是我的spec_helper.rb的一部分:RSpec.configuredo|config|config.before(:each)dologin(email,password)visitroot_urlendend我在所有(20多个)测试中都需要,除了一个。有没有办法避免单一测试执行beforehook? 最佳答案 您可以将元数据添加到不需要登录的测试中,然后在您的beforeHook中评估该元数据。例如,同一个文件中的两个测试。一个需要登录,一个不需要。#foo_spec.rbdescribeFoododescribe"#b