jjzjj

Factory_method_pattern

全部标签

ruby-on-rails - rails : Methods shared by multiple controllers

我有两个Controller,即1)carts_controller2)订单ControllerclassCartsController注意:method3正在使用method1和method2。CartsController有showcart.html.erbView,它使用method3并且工作正常。现在在订单View中,我需要显示购物车(showcart.html.erb),但由于辅助程序method3是在carts_controller中定义的所以它无法访问它。如何解决? 最佳答案 由于您使用的是Rails4(这种方法应该也

ruby - “运行失败跳转到方法定义”错误 : undefined method `current_line' for TextMate:Module

更新:我想通了。Ctrl-F仅在未选择我正在搜索的方法时有效。游标只需要在方法名中。我刚升级到TextMate2。当我选择一个方法并使用Ctrl+F转到它的定义时,我得到:>FailurerunningJumptoMethodDefinition这是痕迹:/Users/ilikepie/Library/ApplicationSupport/TextMate/Managed/Bundles/RubyonRails.tmbundle/Support/lib/rails/text_mate.rb:54:in`method_missing':undefinedmethod`current_li

ruby - 如何在 ruby​​ 中组合包含 method_missing 的模块

我有几个模块缺少扩展方法:moduleSaysHellodefrespond_to?(method)super.respond_to?(method)||!!(method.to_s=~/^hello/)enddefmethod_missing(method,*args,&block)if(method.to_s=~/^hello/)puts"Hello,#{method}"elsesuper.method_missing(method,*args,&block)endendendmoduleSaysGoodbyedefrespond_to?(method)super.respond_

ruby-on-rails - 如何在 Factory Girl 或 Minifacture 中使用随机唯一数据制作 Ruby 测试工厂?

我正在用一个典型的工厂测试一个典型的Rails模型:#Mymodelusesa3-letteruppercaseairportcode,#suchas"ATL"forAtlanta,"BOS"forBoston,etc.classAirport我正在添加更多测试并开始看到机场代码中的冲突:例如,工厂创建了一个代码为“XYZ”的机场,然后对工厂的后续调用尝试创建一个具有相同代码的机场。序列是解决这个问题的一种方法。例如,使用FactoryGirl序列、有序列表或预先计算的枚举,以及维护下一个可用代码状态的类似方法。我的问题是:解决这个问题的非序列方法是什么?我想使用随机数据,不是序列。我

ruby-on-rails - 参数错误 : Factory not registered

我正在尝试使用FactoryGirl运行rspec,但我一直收到此错误:1)ProductsUpdatewithinvalidinformationFailure/Error:let(:product){FactoryGirl.create(:product)}ArgumentError:Factorynotregistered:product#./spec/requests/products_spec.rb:47:in`block(3levels)in'#./spec/requests/products_spec.rb:52:in`block(3levels)in'-这是有错误的测试

ruby-on-rails - 如何指定与factory_bot的关联?

例如我有两个模型一个用户和一个帖子。一个帖子属于_一个用户,一个用户有很多帖子#spec/factories/post.rbFactoryBot.definedofactory:postdouserbodyFaker::Movie.quoteposted_at"2018-04-0313:33:05"endend#spec/factories/user.rbFactoryBot.definedofactory:userdofirst_name'Jake'endend在测试中使用Rspec我想这样做:user=create(:user,first_name:'Barry')#id1post

ruby-on-rails - 使用字符串属性键而不是符号的 Factory Girl?

当我调用Factory.attributes_for(:some_class)时,我显然得到了该类的属性散列。{:attribute_one=>"hello",:attribute_two=>"goodbye"}有没有一种方便的方法来使用字符串键而不是符号来检索此属性散列?{"attribute_one"=>"hello","attribute_two"=>"goodbye"} 最佳答案 xdazz的答案是一个不错的选择,但如果您想实际将键转换为字符串而不是无动于衷地访问哈希,您可以使用stringify_keysFactory.a

ruby-on-rails - 如何捕获 "undefined method ` [ ]' for nil:NilClass"错误?

我通过omniauth从facebook获得了一个嵌套数组,想检查它是否为空?/nil?/exists?依赖行看起来像:unlessomniauth['extra']['raw_info']['location']['name'].nil?这应该检查数组的这一部分是否为空或存在。但总是抛出这个错误:undefinedmethod`[]'fornil:NilClass我检查数组有误吗?我试过用“has_key”“nil?”“空的?”“存在?”“空白?”但这些都行不通!请帮助我,非常感谢! 最佳答案 理想情况下,您应该检查每个嵌套级别以

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 - 为什么没有 Module.method_defined?(:method) work correctly?

我正在尝试使用Module.method_defined?(:method)检查模块中是否定义了方法,它返回false,应该返回true。moduleSomethingdefself.another1endendSomething.methods列出了“另一个”,但Something.method_defined?(:another)返回false。这可能不起作用,因为该方法是在self上定义的吗?如果是这种情况,除了使用method_defined?之外,还有其他方法可以检查模块上是否定义了方法吗? 最佳答案 要知道模块是否有模块