jjzjj

substring-before

全部标签

ruby-on-rails - skip_before_action 和 Rails 5

我刚刚升级到Rails5,一切都非常顺利,但没有明显的原因,在skip_before_action之后调用的方法不允许rspec运行此消息Beforeprocess_actioncallback:redirect_heroku_userhasnotbeendefined(ArgumentError)这太奇怪了,因为它在rails4上工作得很好。这是我的代码:#application_controller.rbdefredirect_heroku_userredirect_toroot_pathifheroku_user?end#some_controller.rbskip_before

ruby-on-rails - rails 是如何实现 before_filter 的?

我对Rails如何实现像before_filter这样的过滤器很感兴趣。但是看了源码还是一头雾水。我注意到rails的框架维护了一个filter_chain,并在目标方法之前运行过滤器。但是,我不明白一个重要的过程:rails是如何捕获方法调用的?我的意思是,例如,我有一个类Dog,并为方法bark设置了一个before_filter。当我调用dog.bark时,rails应该以某种方式捕获此调用,并改为运行其修改后的方法。但是,我在源代码中没有找到这样的代码。任何人都可以告诉我这个想法或指出代码所在的位置吗? 最佳答案 当您设置b

ruby - rails 3 : Do i need to give return true in a before_save callback for an object. 保存工作?

ClassUserbefore_save:set_searchabledefset_searchableself.searchable=trueifself.status==:activeendend>>u=User.last>>u.savefalseu.save总是返回false。如果我删除before_save它会起作用另外,如果我在before_save中返回true,它也有效所以我需要在before_save中给出return语句吗?如果before_save返回false,ActiveRecord会保存一个对象吗?我在哪里可以看到有关回调及其工作流程的完整文档。提前致谢

使用substring断开列

我在列中有很长的字符串作为员工的全名,我想以名称,中间名和姓氏分解。例如我有Andrade,MariaSandra(lname,(space)fname(space)mname)破裂将就像fname=玛丽亚,lname=andrade和中间名,例如桑德拉(Sandra)。我的专栏通常有lname,fname。我可以分割lname,但不能正确地打破fname和mnameSELECTEmployeeNameas[fullname],SUBSTRING(EmployeeName,CHARINDEX(',',EmployeeName)+1,LEN(EmployeeName))as[Firstname]

ruby-on-rails - 从 before(:each) block) 中获取完整的 RSpec 测试名称

RSpec允许您通过执行以下操作在before(:each)block中获取当前运行的测试方法名称:Spec::Runner.configuredo|config|config.before:eachdo|x|x.method_name#returns'shouldbecool'endend这是为了这样的测试:requireFile.expand_path(File.dirname(__FILE__)+'/../spec_helper')describe'Helloworld'doit'shouldbecool'do#testcodeendend是否有可能在beforeblock中获得

ruby - 反对在 RSpec 测试中使用 before、let 和 subject 的理由是什么?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我刚开始编写RSpec测试,我遇到了thoughtbot'sStyleGuide,它反对let、let!、before和subject(以及其他)。我也在其他几个地方读过类似的建议(包括关于before(:all)的旧RSpecdocs警告),但我似乎找不到反对的实际论据他们。那么问题是:为什么我不应该在我的测试中使用这些方法?什么是更好的方法?

ruby - 在 Sinatra 中,你如何制作一个 "before"过滤器来匹配除某些路由之外的所有路由

我有一个RubySinatra应用程序,我有一些代码需要在除少数异常(exception)情况外的所有路由上执行。我该怎么做?如果我想在选定的路由(白名单样式)上执行代码,我会这样做:['/join',"/join/*","/payment/*"].eachdo|path|beforepathdo#somecodeendend我该如何反其道而行之(黑名单样式)?我想匹配除'/join'、'/join/*'和'/payment/*'之外的所有路由 最佳答案 负面前瞻:before/^(?!\/(join|payment))/do#..

ruby-on-rails - 有没有办法使 before_save 成为条件?

我正在尝试在Rails应用程序中进行有条件的before_save,但它似乎不起作用。before_savemethod_call_to_runifself.related_model.some_method_that_returns_t_or_f?如果“some_method_that_returns_t_or_f”返回true,我希望它在保存对象之前运行该方法,否则我只希望它忽略before_save。 最佳答案 你可以使用:ifbefore_savedo_something,:if=>Proc.new{|model|model

ruby-on-rails - Rails before_filter 用于 Controller 中的特定操作

defnewbefore_filterdoredirect_to"/"unlesscurrent_admin||current_companyflash[:notice]='Youdonthaveenoughpermissionstobehere'unlesscurrent_admin||current_companyendCODECODECODEenddefeditbefore_filterdoredirect_to"/"unlesscurrent_admin.id=5flash[:notice]='Youdonthaveenoughpermissionstobehere'unles

ruby-on-rails - 是否可以在模块中定义 'before_save' 回调?

是否可以在模块中定义before_save回调?这样的类是这样的:classModelincludeMongoMapper::DocumentincludeMyModuleend和这样的模块:moduleMyModulebefore_save:do_somethingdefdo_something#dowhateverendenddo_something会在保存任何Model对象之前调用吗?我试过这样但是得到了undefinedmethod'before_save'forMyModule:Module。抱歉,如果事情很简单-我是Ruby和Rails的新手。