草庐IT

object_attribute_hash

全部标签

ruby-on-rails - Rails 4 强参数 : can I 'exclude' /blacklist attributes instead of permit/whitelist?

我正在将Rails3应用程序迁移到Rails4,并且正在将attr_accessible属性转换为Controller中的强参数。APIDocumentation显示如何“允许”属性:defperson_paramsparams.require(:person).permit(:name,:age)end然而,我的绝大多数属性都是批量分配安全的。我只需要将:account_id和:is_admin等几个属性列入黑名单。是否可以将属性列入黑名单而不是将几乎所有属性列入白名单?例如:defuser_paramsparams.require(:user).exclude(:account_i

ruby-on-rails - 将 Hash 更改为 ruby​​ 中的数组数组

假设我有一个散列{:facebook=>0.0,:twitter=>10.0,:linkedin=>6.0,:youtube=>8.0}现在我想把它变成一个像这样的数组[[Facebook,0.0],[Twitter,10.0],[Linkedin,6.0],[Youtube,8.0]]我可以使用逻辑来提取并将其更改为数组,但我只是想知道在ruby​​中是否有任何定义的方法可用于实现上述内容。 最佳答案 您可以使用to_a.{:facebook=>0.0,:twitter=>10.0,:linkedin=>6.0,:youtube=

ruby-on-rails - 错误 : When assigning attributes, 您必须将散列作为参数传递

嗨,我刚开始使用ruby​​,我正在编写Controller和Controller规范,但我遇到了一些问题。文档.rbclassDocument文档Controller.rbclassAPI::DocumentsControllerdocuments_controller_spec.rbdescribe"POST'index'"dobefore{@attr=FactoryGirl.attributes_for(:document)}describe"failure"dodescribe"withmissingparameters"dobefore{@attr.each{|key,val

ruby - 将 "Growing Object-Oriented Software"技术应用于 Ruby on Rails

我读了GrowingObject-OrientedSoftware,GuidedbyTests史蒂夫·弗里曼和纳特·普赖斯的作品,给人留下了深刻的印象。我想在我使用RSpec的Rails项目中采用本书的思想,尽管它的示例是用Java编写的。本书的一个基本原则是我们应该模拟接口(interface)而不是具体类。他们说我们可以通过提取接口(interface)并命名来改进应用设计。但是,Ruby没有任何语法等同于Java的接口(interface)。我如何将他们的技术用于Rails项目?更新例如,在第126页作者引入了Auction接口(interface)来实现bid方法。首先,他们模

ruby /Rspec : Possible to compare the content of two objects?

我在ruby​​中创建了2个不同的对象,它们具有完全相同的属性和值。现在我想比较两个对象的内容是相同的,但进行以下比较:actual.should==expectedactual.shouldeq(expected)actual.should(beexpected)失败:Diff:@@-1,4+1,4@@-#在rspec/ruby中有什么方法可以轻松实现这一点吗?干杯! 最佳答案 执行此操作的惯用方法是覆盖#==运算符:classStationdef==(o)primary_key==o.primary_keyenddefhashp

ruby-on-rails - 为什么我会收到此 'can' t modify frozen hash' 错误?

我有一个Person模型和一个Item模型。一个人有很多元素,一个元素属于一个人。在此代码中,我需要删除一个人的现有项目,并根据参数(这是一个哈希数组)创建新项目。然后,我需要根据项目的其他字段之一更新项目的字段之一。@person=Person.find(params["id"])@person.person_items.eachdo|q|q.destroyendperson_items_from_param=ActiveSupport::JSON.decode(params["person_items"])person_items_from_param.eachdo|pi|@per

ruby-on-rails - Rails,activerecord : self[:attribute] vs self. 属性

在Rails中访问事件记录列/属性时,使用self[:attribute]与self.attribute有什么区别?这会影响getter和setter吗? 最佳答案 它们都只是获取属性的方法-它们都只是getter。self.attribtue是一个更“传统”的getter,而self[:attribute]基本上只是[]方法。在使用两者之间切换不会产生任何影响。我建议只使用self.attribute方法,因为它在语法上更好。但是,当其他内容覆盖self.attribute方法时,使用self[:attribute]会派上用场。例

ruby - 是否有与 Rspec 的 “mock().as_null_object” 等效的 Mocha?

是否有与Rspec的“mock().as_null_object”等效的Mocha? 最佳答案 是的。使用“stub_everything()”记录在此处:http://mocha.rubyforge.org/classes/Mocha/API.html#M000004. 关于ruby-是否有与Rspec的“mock().as_null_object”等效的Mocha?,我们在StackOverflow上找到一个类似的问题: https://stackover

ruby - Ruby 的 Object#const_get 是如何工作的?

我最近发现Ruby(2.2.1)有一些“有趣”的行为。moduleFooclassFooendclassBarendendFoo.const_get('Foo')#=>Foo::FooFoo.const_get('Bar')#=>Foo::BarFoo.const_get('Foo::Foo')#=>FooFoo.const_get('Foo::Bar')#=>NameError:uninitializedconstantFoo::Foo::BarFoo.const_get('Foo::Foo::Bar')#=>Foo::BarFoo.const_get('Foo::Foo::Foo:

ruby - 按值对 Hash of Hashes 进行排序(并返回哈希,而不是数组)

我有以下哈希:user={'user'=>{'title'=>{'weight'=>1,....}'body'=>{'weight'=>4,....}........}}是否可以让用户按其子哈希的权重键排序?我查看了Hash.sort,但看起来它返回的是数组而不是我原来的哈希排序。 最佳答案 在Ruby1.9中,Hashes被排序,但是Hash#sort仍然返回Array的Array秒。想象一下!它确实意味着您可以在此基础上构建自己的排序方法。classHashdefsorted_hash(&block)self.class[sor