草庐IT

ne-app-and-server

全部标签

ruby-on-rails - 如何解决弃用警告 "Method to_hash is deprecated and will be removed in Rails 5.1"

我正在尝试更新到Rails5,我收到以下弃用警告:DEPRECATIONWARNING:Methodto_hashisdeprecatedandwillberemovedinRails5.1,asActionController::Parametersnolongerinheritsfromhash.Usingthisdeprecatedbehaviorexposespotentialsecurityproblems.Ifyoucontinuetousethismethodyoumaybecreatingasecurityvulnerabilityinyourappthatcanbee

ruby - 什么是 :+ and &:+ in Ruby?

我已经看过好几次了,但我不知道如何使用它们。镐说这些是特殊的快捷方式,但我找不到语法描述。我在这种情况下见过他们:[1,2,3].inject(:+)例如计算总和。 最佳答案 让我们从一个更简单的例子开始。假设我们有一个我们想要大写的字符串数组:['foo','bar','blah'].map{|e|e.upcase}#=>['FOO','BAR','BLAH']此外,您还可以创建所谓的Proc对象(闭包):block=proc{|e|e.upcase}block.call("foo")#=>"FOO"您可以使用&语法将这样的过程传

ruby-on-rails - rails 验证 : :allow_nil and :inclusion both needed at the same time

通常“种类”字段应允许留空。但如果不为空,则该值应包含在['a','b']validates_inclusion_of:kind,:in=>['a','b'],:allow_nil=>true代码不起作用? 最佳答案 在Rails5中,您可以在包含block的外部或内部使用allow_blank:true:validates:kind,inclusion:{in:['a','b'],allow_blank:true}或validates:kind,inclusion:{in:['a','b']},allow_blank:true提示

ruby-on-rails - rails : respond_to JSON and HTML

我有一个Controller“UserController”,它应该响应对http://localhost:3000/user/3的正常请求和ajax请求。当是正常请求时,我要渲染我的View。当是AJAX请求时,我想返回JSON。正确的方法似乎是respond_todo|format|block。编写JSON很容易,但我怎样才能让它响应HTML并像往常一样简单地呈现View呢?defshow@user=User.find(params[:id])respond_todo|format|format.html{render:show????thisseemsunnecessary.Ca

ruby-on-rails - ruby rails : How do you explicitly define plural names and singular names in Rails?

例如,我使用“Bonus”作为我的模型,所以我希望“bonuses”是复数形式而“bonus”是单数形式。但是,在Ruby中,这会导致:"bonus".pluralize#bonus"bonuses".singularize#bonuse因此,例如,当我执行“has_many:bonuses”时,它不会使用Bonus.rb模型(因为Ruby需要Bonuse.rb模型)。有没有一种方法可以在RubyonRails中以某种方式更正这一点,使“bonuses”充当模型bonus.rb的复数形式? 最佳答案 在config/initiali

ruby-on-rails - Google App Engine 上的 Ruby on Rails 应用程序

谁能给我一些关于如何将我的Rails应用程序部署到GAE的建议?我一直在阅读它,但这似乎是一项相当复杂的任务。我尝试使用google-appenginegem,但它也不是小菜一碟。DataMapper适配器是否有任何进展,或者我是否需要更改我的模型?我希望看到有关它的完整详细教程,但我发现那些有些过时了。 最佳答案 在Google的AppEngine上部署Rails比以前容易得多。您应该注意一些注意事项:AppEngine仅支持Python和Java环境,因此对于Rails,您将在JRuby上部署AppEngine的数据存储基于Bi

ruby-on-rails - '需要': cannot load such file -- 'nokogiri\nokogiri' (LoadError) when running `rails server`

我正在使用DevKit在Windows8.1上运行全新安装的Ruby2.2.1。安装后我运行:geminstallrailsrailsnewtestappcdtestapprailsserver保留其他所有默认值。进程在最后一行失败,我没有运行服务器,而是收到错误消息in'require':cannotloadsuchfile--'nokogiri\nokogiri'(LoadError)每次都会发生这种情况,我环顾四周并尝试了我发现的所有方法来修复它,但到目前为止没有任何效果。这里的问题是什么?如何让一个简单的测试Rails应用程序正常工作? 最佳答案

ruby - 通过 Nginx 的 EventSource/Server-Sent 事件

在服务器端使用带有streamblock的Sinatra。get'/stream',:provides=>'text/event-stream'dostream:keep_opendo|out|connections在客户端:vares=newEventSource('/stream');es.onmessage=function(e){$('#chat').append(e.data+"\n")};当我通过http://localhost:9292/直接使用应用程序时,一切正常。连接是持久的,所有消息都传递给所有客户端。但是,当它通过Nginx时,http://chat.dev,连接

ruby-on-rails - 不兼容的字符编码 : ASCII-8BIT and UTF-8

我使用Ruby1.9.2和Rails3.0.5我有以下错误:incompatiblecharacterencodings:ASCII-8BITandUTF-8我认为这与数据库无关。错误发生在View中的这一行(只是一个divhaml调用):#content全栈:ActionView::Template::Error(incompatiblecharacterencodings:ASCII-8BITandUTF-8):21:-flash.eachdo|name,msg|22:=content_tag:div,msg,:id=>"flash_#{name}"23:%div.clear24:

ruby-on-rails - rails : Validating min and max length of a string but allowing it to be blank

我有一个要验证的字段。我希望该字段能够留空,但如果用户正在输入数据,我希望它采用某种格式。目前我在模型中使用以下验证,但这不允许用户将其留空:validates_length_of:foo,:maximum=>5validates_length_of:foo,:minimum=>5如何编写此代码以实现我的目标? 最佳答案 你也可以使用这种格式:validates:foo,length:{minimum:5,maximum:5},allow_blank:true或者因为您的最小值和最大值相同,以下也将起作用:validates:foo