草庐IT

webkit-column-break-before

全部标签

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 迁移 : tried to change the type of column from string to integer

我使用railsgeneratemigrations命令在我的rails应用程序中创建了一个表。这是迁移文件:classCreateListings然后我想将纬度和经度存储为整数我试着跑:railsgeneratemigrationchangeColumnType该文件的内容是:classChangeColumnType我原以为列类型会发生变化,但是rake被中止并出现了以下错误消息。我想知道为什么这没有通过?我在我的应用程序中使用postgresql。rakedb:migrate==ChangeColumnType:migrating=========================

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 - Rails 4 迁移 : how to reorder columns

我了解到add_column有一个:after选项来设置插入列的位置。太糟糕了,我才知道它:在添加了一堆之后。如何编写迁移以简单地对列进行重新排序? 最佳答案 当使用MySQL时,您可以调用change_column,但是您必须重复列类型(只需从您的其他迁移中复制并粘贴它):defupchange_column:your_table,:some_column,:integer,after::other_columnend或者如果您必须对一个表中的多个列重新排序:defupchange_table:your_tabledo|t|t.c

ruby-on-rails - rails 迁移 : How to increase column data type size by using ROR migration

我的用户表登录列是String类型,限制为40个字符。现在我打算将限制增加到55个字符。任何人请让我知道我们如何通过使用ROR迁移来增加此限制。谢谢,沙湾 最佳答案 classYourMigration55enddefdownchange_column:users,:login,:string,:limit=>40endend 关于ruby-on-rails-rails迁移:HowtoincreasecolumndatatypesizebyusingRORmigration,我们在Sta

ruby-on-rails - Ruby 语法 : break out from 'each.. do..' block

我正在开发一个RubyonRails应用程序。我的问题更多是关于Ruby语法。我有一个带有类方法self.check的模型类:classCars我想在eachblock一旦result为true(即如果car.name与name参数相同一次,则打破eachblock并返回car导致true结果。如何在Ruby代码中打出? 最佳答案 您可以使用break关键字中断。例如[1,2,3].eachdo|i|putsibreakend将输出1。或者如果你想直接返回值,使用return。由于您更新了问题,这里是代码:classCar尽管您也可

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

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