草庐IT

Time_of_Sail

全部标签

ruby-on-rails - rails : render a collection of models using an specific html view

我有以下关于rails的简单问题。假设我有一个模型用户。在View中,如果我这样做:views/user/_user.html.erb中的文件View将为每个用户调用和打印。如何更改它以使用特定View?我需要这样的东西:User.all:template=>"user/_user_2ndview.html"%>有什么帮助吗?提前致谢 最佳答案 您可以使用collection选项:User.all,:partial=>"users/user2ndview",:as=>:user%>View必须放在views/users/_user2

ruby-on-rails - Ruby 类评估,validates_inclusion_of 与动态数据

如果我有如下的ActiveRecord模型classFooself.allowed_typesdefself.allowed_types#somecodethatreturnsanenumerableendend这不起作用,因为在评估验证时尚未定义allowed_types方法。我能想到的所有修复基本上都是围绕将方法定义移到验证之上,以便在需要时可用。我明白这可能更像是一个编码风格问题(我希望我的所有验证都在模型顶部,方法在底部)但我觉得应该有某种解决方案,可能涉及初始模型加载的惰性评估?我想做的事有可能吗?我应该只在验证之上定义方法,还是有更好的验证解决方案来实现我想要的。

ruby-on-rails - Capistrano gzip : stdin: unexpected end of file interruption

我在Capistrano部署方面遇到问题。部署过程因Gzip错误而被丢弃。它可以是什么?capflowdeploy*executing`flow'*executing`deploy'*executing`deploy:update'**transaction:start*executing`deploy:update_code'*getting(viacheckout)revisionto/tmp/20111214204507executinglocally:cp-R./tmp/20111214204507commandfinishedin413mscompressing/tmp/20

ruby-on-rails - rails : Prevent will_paginate from calling #count of ActiveRelation

当我向will_paginate传递一个ActiveRelation时,它总是调用它的#count方法并访问数据库以找出项目的总数。但是这个操作需要时间,而且我已经缓存并准备好了总数。我可以将这个预先计算的计数传递给will_paginate并阻止它访问数据库吗?我尝试了:count选项,但它作为选项传递给了ActiveRecord:active_relation.paginate(page:2,per_page:100,count:total_count)谢谢!:) 最佳答案 通过:total_entries传递缓存计数解决了问题

ruby-on-rails - 带分号的字符串在 RubyMine 中产生 "unterminated string meets end of file"

在RubyMine调试器中,只需在watch中输入:';'或";"我收到错误:"unterminatedstringmeetsendoffile"这是为什么?据我所知,它不会发生在Rails控制台中,并且与RubyMine没有任何关系。 最佳答案 这是Ruby调试器与Ruby解释器具有不同解析规则的结果。事实上,从irb或ruby命令调用的常规Ruby调试器表现出相同的行为。然而,解决方法很简单:要创建一个由单个分号组成的字符串文字,只需使用反斜杠将其转义即可:$irb>require'debugger'=>true>debugge

Ruby 设置类 : equality of sets

根据RubySet类的文档,“==如果两个集合相等,则返回true。每对元素的相等性根据Object#eql?定义。可以使用Date对象来演示其本质,其中包含不同Date对象但具有相同日期的集合比较相等:require'set'd1=Date.today#=>Thu,30Sep2010putsd1.object_id#=>2211539680d2=Date.today+1#=>Fri,01Oct2010putsd2.object_id#=>2211522320set1=Set.new([d1,d2])d11=Date.today#=>Thu,30Sep2010putsd11.objec

ruby-on-rails - validates_associated 和 validates_presence_of 没有按预期与 rspec 一起工作?

我有一个用户模型和一个友谊模型。classFriendship'User',:foreign_key=>'sender_id'belongs_to:receiver,:class_name=>'User',:foreign_key=>'receiver_id'validates_presence_of:receiver_id,:sender_idvalidates_associated:receiver,:senderendclassUser"Friendship",:foreign_key=>"sender_id",:dependent=>:destroyhas_many:recei

ruby - 应该 validate_format_of 。 not_with 在框架中有问题(或者在我的理解中)

我将以下代码放入RSpec测试中:it{shouldvalidate_format_of(:email).not_with('test@test')}并设置实际的类:validates:email,:presence=>true,:format=>/\b[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i当我运行测试时,我得到:失败:1)用户失败/错误:它{应该validate_format_of(:email).not_with('test@test')}当电子邮件设置为“test@test”时,预期错误包括“can'tbeblank”,得到错误

ruby - 欧拉计划 1 :Find the sum of all the multiples of 3 or 5 below 1000

我正在尝试使用ProjectEuler中的Ruby解决数学问题。Here是我尝试的第一个:Ifwelistallthenaturalnumbersbelow10thataremultiplesof3or5,weget3,5,6and9.Thesumofthesemultiplesis23.Findthesumofallthemultiplesof3or5below1000.请帮助我改进我的代码。total=0(0...1000).eachdo|i|total+=iif(i%3==0||i%5==0)endputstotal 最佳答案

ruby-on-rails - 我得到 rvm : command not found after installation of rvm

我知道上面的问题是很常见的问题。我已经阅读了关于这个主题的多篇文章。但是我没有得到任何解决方案。我已经在本地安装了rvm。我们已经有了安装文件。所以进入文件夹并运行安装命令。$./install然后我检查了Users主文件夹中的./rvm文件夹$cd~/.rvm文件夹存在。至此安装成功。现在我在命令行输入rvm$rvm我正在低于异常$rvm-sh:rvm:commandnotfound在阅读了stackoverflow中有关此问题的多篇文章后,我了解到我必须在.bash_profile中添加以下行,因为我使用的是MacOSX10.7.3[[-s"$HOME/.rvm/scripts/r