草庐IT

ruby-on-rails - rails : Plus sign in GET-Request replaced by space

在Rails3(Ruby1.9.2)中我发送一个请求StartedGET"/controller/action?path=/41_+"但是参数列表是这样的:{"path"=>"/41_","controller"=>"controller","action"=>"action"}这里出了什么问题?-、*或.符号工作正常,只是+将被空格替换。 最佳答案 这是正常的URL编码,theplussignisashorthandforaspace:Withinthequerystring,theplussignisreservedasshor

ruby-on-rails - railstutorial.org 中的 SessionsHelper : Should helpers be general-purpose modules for code not needed in views?

railstutorial.org有一个让我觉得有点奇怪的建议。Itsuggeststhiscode:classApplicationControllerincludeSessionsHelper使方法在ApplicationController中可用,是的,但它也使它们在任何View中都可用。我知道身份验证/授权是交叉的,但这真的是最好的地方吗?在我看来,这可能范围太广了。将实现有条件重定向(如railstutorial.org示例所做的)的before_filter的代码放在更通常包含View助手的模块中似乎令人惊讶。将View中不需要的功能放在ApplicationControl

ruby - 用 RSpec 打桩 Time.now

我正在尝试在RSpec中对Time.now进行stub,如下所示:it"shouldsetthedatetothecurrentdate"do@time_now=Time.nowTime.stub!(:now).and_return(@time_now)@thing.capture_item("description")expect(@thing.items[0].date_captured).toeq(@time_now)end这样做时出现以下错误:Failure/Error:Time.stub!(:now).and_return(@time_now)NoMethodError:un

ruby-on-rails - Ruby Time.parse 给我超出范围的错误

我正在使用Time.parse从字符串创建时间对象。出于某种原因Time.parse("05-14-200919:00")导致参数我们的范围错误,而Time.parse("05-07-200919:00")没有有什么想法吗? 最佳答案 如果您知道字符串的格式,请使用:Time.strptime(date,format,now=self.now){|year|...}http://www.ruby-doc.org/core-1.9/classes/Time.html#M000266它将解决您的问题,并且可能比Time.parse更快。

ruby-on-rails - 抽取 Assets :precompile gets killed when there is a console session open in production

在我托管在digitalocean上的生产服务器上,如果有帮助的话,Ubuntu12.04,我有RoR4和rake10.1.1。当我部署时,我运行rakeassets:precompile,我注意到一个奇怪的问题,如果我在执行此操作时打开了一个railsconsolesession,我得到以下输出~#rakeassets:precompile~#Killed主要是很烦人,但我希望解决这个问题的原因是在雇用新开发人员时,会有部署/控制台冲突噩梦。谢谢,布莱恩 最佳答案 您的预编译进程可能被终止,因为您的内存不足。您可以通过在另一个ss

ruby-on-rails - ActionMailer 最佳实践 : Call method in the model or the controller?

发送电子邮件通常在对模型执行操作后调用,但电子邮件本身是一个View操作。我正在寻找您如何考虑要问自己什么问题来确定将操作邮件程序方法调用放在何处。我见过/使用过它们:在模型方法中-相关但独立的关注点的耦合不良?在模型的回调中(例如after_save)-就我目前的知识水平而言,最好的分离。在Controller操作中-只是感觉不对,但在某些情况下这是构建代码的最明智的方式吗?如果我想知道如何编程,我需要像程序员一样思考,因此学习如何思考特定的编程解决方案值得我独自编码数月。谢谢! 最佳答案 迟到的答案,但我想在这个问题上合理化:通

ruby `split' : invalid byte sequence in UTF-8 (ArgumentError)

我正在尝试填充电影对象,但在解析u.item文件时出现此错误:`split':invalidbytesequenceinUTF-8(ArgumentError)File.open("Data/u.item","r")do|infile|whileline=infile.getsline=line.split("|")endend仅当尝试使用花哨的国际标点符号拆分行时才会发生错误。这是一个例子543|Misérables,Les(1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Mis%E9rables%2C%20Les%20%281

ruby - rails 3 : Do i need to give return true in a before_save callback for an object. 保存工作?

ClassUserbefore_save:set_searchabledefset_searchableself.searchable=trueifself.status==:activeendend>>u=User.last>>u.savefalseu.save总是返回false。如果我删除before_save它会起作用另外,如果我在before_save中返回true,它也有效所以我需要在before_save中给出return语句吗?如果before_save返回false,ActiveRecord会保存一个对象吗?我在哪里可以看到有关回调及其工作流程的完整文档。提前致谢

ruby-on-rails - 是什么导致弃用警告:ActiveRecord::Base.raise_in_transactional_callbacks=?

我在运行功能规范时收到此消息:DEPRECATIONWARNING:ActiveRecord::Base.raise_in_transactional_callbacks=isdeprecated,hasnoeffectandwillberemovedwithoutreplacement.我正在使用Rails5.0.0.rc1,我不确定是什么引发了这个弃用警告。我的application.rb文件中有这个。我删除了它,弃用警告消失了:config.active_record.raise_in_transactional_callbacks=true我想了解此弃用警告的实际含义以及触发此

ruby-on-rails - rails : how to show user's "last seen at" time?

我正在使用存储current_sign_in_at和last_sign_in_at日期时间的设备。但是假设用户在一个月前登录但最后一次查看页面是在5分钟前?有什么方法可以显示(“5分钟前用户最后一次出现”)。 最佳答案 这个怎么样:创建迁移以向用户添加新字段以存储用户最后一次出现的日期和时间:railsgmigrationadd_last_seen_at_to_userslast_seen_at:datetime向您的应用程序Controller添加一个操作前回调:before_action:set_last_seen_at,if: