草庐IT

Collection-hooks

全部标签

ruby-on-rails - Redmine Hook 的验证问题

我在编写Redmine插件时遇到验证问题。我正在为问题模型编写一个Hook,作为Hook方法的一部分,我想通过添加自定义错误来使问题的创建无效:defcontroller_issues_new_before_save(context={})context[:issue].errors.add(:due_date,"Acustomerror")end出于测试目的,我写了一个覆盖Issue.validate_on_create的补丁,但似乎每次输入validate_on_createerrors.count都设置为零。我需要停止问题对象的创建,但只有当一个属性被设置到另一个模型对象时。我想

ruby-on-rails - 如何将 collection_check_boxes 默认选中?

我试图默认选中这行在dbt.text"committed".我尝试了checked的变体&true,但也许我忽略了一些东西。这是Gist的。 最佳答案 这里是关于如何将选中作为默认值添加到collection_check_boxes表单助手的快速回答,因为我花了一些时间才弄明白。把它分成一个block,你可以设置检查和添加类。更多信息,请访问http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_

ruby - 如何编写一个在安装 RubyGem 时调用的 Hook ?

我想编写一个Ruby片段,当我的Gem首次通过[sudo]geminstallmygem安装时运行。可以吗? 最佳答案 好像真的不支持。我发现了一个“post_install_message”属性,您应该可以在gem规范中设置它,但它不会执行代码。您可以通过将安装代码打包为gem中的扩展(就好像它是native扩展)并提供Rakefile来“构建”扩展(即调用您的代码)来实现。 关于ruby-如何编写一个在安装RubyGem时调用的Hook?,我们在StackOverflow上找到一个类

ruby - 导轨 3.2 : how to find all parents from a collection of children

给定一组“书籍”,找到所有“作者”(无重复)的最佳方法是什么?假设我们有一个经典关联:classAuthor我现在的做法是这样的:@books=Book.where("somecondition")@authors=Author.where(:id=>@books.map(&:author_id))有没有更好的方法呢? 最佳答案 这里有一个方法:@books=Book.where("somecondition").includes(:author)@authors=@books.map(&:author).compact.uniq解

ruby-on-rails - Minitest 和设置/拆卸 Hook

我在test_helper中有以下代码require"minitest/spec"require"minitest/autorun"require"database_cleaner"classActiveSupport::TestCaseDatabaseCleaner.strategy=:deletionincludeMinitest::Spec::DSLsetup{DatabaseCleaner.start}teardown{DatabaseCleaner.clean}end如果我写这样一个测试classMyTest执行安装和拆卸但是如果我这样写测试classMyTest不执行设置和

Ruby:如何 Hook 类方法

发件人:http://cheind.blogspot.com/2008/12/method-hooks-in-ruby.html我有#ContainsmethodstohookmethodcallsmoduleFollowingHookmoduleClassMethodsprivate#Hooktheprovidedinstancemethodssothattheblock#isexecuteddirectlyafterthespecifiedmethodshave#beeninvoked.#deffollowing(*syms,&block)syms.eachdo|sym|#Fore

ruby - 为什么我的 Ruby Git 脚本 Hook 使用错误的 $PATH 运行?

我正在使用RVM。我为一个项目写了一个Gitpre-commit钩子(Hook):#!/usr/bin/envrubyputsRUBY_VERSIONputs`echo$PATH`exit(1)Git运行时输出:$gitci-m'foo'1.8.7/usr/libexec/git-core:/usr/bin:/usr/local/heroku/bin:/Users/mgoerlich/.rvm/gems/ruby-2.0.0-p195@global/bin:/Users/mgoerlich/.rvm/rubies/ruby-2.0.0-p195/bin:/Users/mgoerlich

ruby-on-rails - rails 5 : How to pass collection_select values through strong_params in a fields_for?

我已经尝试了所有视频和文章,但仍然找不到将fields_forcollection_select值加入strong_params白名单的解决方案。我花了好几天时间想弄明白(并问了很多人)。如果有人能花时间帮助我,我将不胜感激!我在List和Topic之间有一个多对多的关联,List_Topic充当连接模型。使用form_for,我为List的一个实例(@list)创建了一个表单,然后是一个fields_for:list_topics。在字段中,我创建了一个collection_select,它由Topic.all填充。在我的列表Controller中我有:classListsContr

Ruby:如何通过继承 Hook 回调

我在使用Ruby时遇到了一些关于回调(和继承)的问题。这是我的代码:classLmaodefinitialize@str="HAHAHAHAHAHHAHAHAH"@before_laughing=[]enddefself.inherited(base)base.extend(Callbacks)enddeflaughing@before_laughing.each{|method|send(method)}@strendendmoduleCallbacksdefbefore_laughing(*methods)@before_laughing=methodsendendclassLol

ruby - RVM after_cd Hook

我正在尝试使用RVMhooks在我使用Rails应用程序cd进入目录后运行命令。我的~/.rvm/hooks/after_cd的内容是:echo"Nowusing$rvm_ruby_string"我的~/.rvm/hooks/after_use的内容是:echo"Nowusing$rvm_ruby_string"当我执行$rmvuse1.9.2时,我看到了我的回声,但是当我cd进入我的应用程序的rails根目录时,我没有得到任何回声.我使用的after_cd钩子(Hook)错了吗?$rvm-v说:rvm1.0.8byWayneE.Seguin(wayneeseguin@gmail.co