jjzjj

first-instance_method

全部标签

ruby-on-rails - Rails 茧 gem : Undefined Method 'new_record?' on link_to_remove_association with Wicked

我一直在努力弄清楚一些代码。我有一个表格,我正在尝试使用Wicked和茧gem。一切正常,包括link_to_add_association功能。正如Cocoon所建议的那样,我正在为关联的表单字段呈现部分内容,并且除了link_to_remove_association函数之外,一切似乎都正常工作。它返回以下错误:nil:NilClass的未定义方法new_record?这是我抛出错误的部分:这是调用部分的View:location%>这是调用View的Controller操作:classUserStepsController如果有帮助,这里是cocoon中抛出错误的函数:defli

ruby-on-rails - 助手设计 : could not find the `Warden::Proxy` instance on request environment

我尝试将Devise用于我的Rails应用程序。我可以注册并登录,但是当我转到其他页面“构建”时,出现以下错误:Devise::MissingWardeninHome#showDevisecouldnotfindtheWarden::Proxyinstanceonyourrequestenvironment.MakesurethatyourapplicationisloadingDeviseandWardenasexpectedandthattheWarden::Managermiddlewareispresentinyourmiddlewarestack.Ifyouareseeing

Ruby block 、procs 和 instance_eval

我最近尝试做类似的事情:a="somestring"b=Proc.new{upcase}a.instance_evalb这给出了错误:TypeError:can'tconvertProcintoString但这行得通:defb(&block)"somestring".instance_eval&blockendb{upcase}进一步了解此方法:defb(&block)"somestring".instance_evalblockend产生相同的ProctoString错误。所以...我对方block的理解是它们只是过程。但是显然,使用这个&符号有一些特别之处......有人能给我解释

ruby-on-rails - Rails 的 ActiveRecord 序列化 :attr method gives "Missing Class or module error"

我试图在ActiveRecord模型中序列化一个简单的属性,而Rails2.3.4不喜欢它。classShopperserialize:tagsend>>a=Shopper.new=>>>a.tags=['aoeu','stnh']=>['aoeu','snth']>>a.save=>TypeError:classormodulerequired有人知道我错过了什么吗? 最佳答案 Arf...我以为我可以一次序列化两个属性,但事实并非如此:serialize:tags,:garments#thisiswrong第二个参数应该是序列化

ruby-on-rails - self.send(method, =, value) 不起作用

我正在尝试创建一个Ruby类,其中initialize方法接受选项的散列。然后,我将这些选项作为类的attr_accessor。现在,我可以做类似的事情classUserattr_accessor:name,:email,:phonedefinitialize(options)self.name=options[:name]self.email=options[:email]self.phone=options[:phone]endendUser.new(:name=>'SomeName',:email=>'some-name@some-company.com',:phone=>435

ruby - 总新手 : Instance variables in ruby?

请原谅新手问题,但为什么@game_score总是零?#bowling.rbclassBowling@game_score=0defhit(pins)@game_score=@game_score+pinsenddefscore@game_scoreendend 最佳答案 让我们看一下代码,好吗?#bowling.rbclassBowling@game_score=0#(1)此时(1),我们仍在classBowling中。记住:类和其他对象一样只是对象。因此,此时您将0分配给类对象Bowling的实例变量@game_score。de

ruby-on-rails - class_methods 在关注中做了什么?

我正在阅读一些使用Rails4中关注点的代码。我看了一些文章说,如果我们想包含类方法使用模块ClassMethods,但我阅读的代码使用类似:class_methodsdodef****endend 最佳答案 ActiveSupport::Concern为模块混合的常见Ruby模式提供语法糖。当您使用modulesasmixins时你不能像在类中那样只使用self来声明类方法:moduleFoodefself.bar"HelloWorld"enddefinstance_method"HelloWorld"endendclassBaz

ruby - 试图理解 self.method_name 与 Classname.method_name 在 Ruby 中的使用

我试图了解何时使用self.method_name与何时使用Classname.method_name。在下面的示例中,为什么“before_create”需要引用“User.hash_password”而不是“self.hash_password”或只是“hash_password”?由于我们已经在User类中,我认为before_create方法会“知道”“hash_password”是它自己的类的成员,不需要任何特殊语法来引用它。require'digest/sha1'classUser["name=?andhashed_password=?",name,hashed_passw

ruby-on-rails - ruby /RoR : calling original method via super()?

在RoR应用程序中,我想在我的一个模型中专门化ActiveRecord的update_attributes()方法,提取一些属性进行特殊处理,并将其余属性传递给原始的update_attributes()方法。详情:classPremise对super.update_attributes(attr)的调用引发错误:undefinedmethod`update_attributes'fortrue:TrueClass...这让我怀疑我真的不了解Ruby中的super关键字。我错过了什么?具体来说,如何调用原始的update_attributes()方法? 最佳

ruby - RSpec any_instance 返回 self

我正在尝试stub某个类的任何实例。我需要stubfetch方法,它用一些数据填充self。如何访问self变量,修改它并返回fetch方法?MyObject.any_instance.stub(:fetch){self}不返回MyObject实例。也许,模拟在这种情况下更有用。不幸的是,我还没有理解它们。 最佳答案 有一个openrspec-mocksissue解决这个问题。我希望在某个时候解决这个问题,但是以一种不破坏现有规范套件的方式添加它并不简单,这些规范套件使用any_instanceblock实现,因为我们会开始屈服一个