草庐IT

chain-of-responsibility

全部标签

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 - 没有将 HTTParty::Response 隐式转换为字符串

当我尝试将某种响应解析为JSON时,出现以下错误。如果万一解析失败,我会在我的代码中引发JSON::ParserError。但是这种异常不属于这个解析器错误。不知道为什么会抛出这种错误?以及如何挽救这个错误?代码:beginparsed_response=JSON.parse(response)rescueJSON::ParserError=>enilend错误:ATypeErroroccurredinbackgroundat2014-11-1603:01:08UTC:noimplicitconversionofHTTParty::ResponseintoString

ruby-on-rails - 在 Rails 中,我可以在它返回之前在 Action 中访问 response.body 吗?

在Rails中,我可以在action返回之前访问response.body吗?假设我想在它返回之前做一些最终的字符串替换,我可以访问response.body,即View返回的响应吗? 最佳答案 在你的Controller中尝试after_filter。您应该可以从那里编辑您的response.body。对我来说,我需要删除xml中的一些ASCII字符,因此我这样做了。after_filter:sanitize_xmldefsanitize_xml#cleantheresponsebodybyaccessingresponse.bo

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”,得到错误