草庐IT

bin_to_float

全部标签

ruby 模板 : How to pass variables into inlined ERB?

我有一个内联到Ruby代码中的ERB模板:require'erb'DATA={:a=>"HELLO",:b=>"WORLD",}template=ERB.newcurrentvalueis:EOFDATA.keys.eachdo|current|result=template.resultoutputFile=File.new(current.to_s,File::CREAT|File::TRUNC|File::RDWR)outputFile.write(result)outputFile.closeend我无法将变量“current”传递到模板中。错误是:(erb):1:undefi

ruby-on-rails - accepts_nested_attributes_for 和 belongs_to 多态

我想用accepts_nested_attributes_for建立一个多态关系。这是代码:classContact:clientendclassJob:trueaccepts_nested_attributes_for:clientend当我尝试访问Job.create(...,:client_attributes=>{...}时给我NameError:uninitializedconstantJob::Client 最佳答案 我也遇到了“ArgumentError:无法构建关联模型名称。您是否正在尝试构建多态一对一关联?”的问题

ruby-on-rails - 监听错误 : unable to monitor directories for changes

在Ubuntu服务器上运行我的Rails应用程序时出现以下错误FATAL:Listenerror:unabletomonitordirectoriesforchanges.Visithttps://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchersforinfoonhowtofixthis.我已经关注了上面的GitHub页面,但是我无法写入在8192中设置的max_user_watches,我想将其设置为524288。在cat/proc/sys/fs/inotify/max_user_watche

ruby - 如何使用 link_to Ruby on rails 添加确认消息

我想使用Ruby在link_to函数上添加确认消息。=link_to'Resetmessage',:action=>'reset',:confirm=>'Areyousure?'知道为什么它不起作用吗? 最佳答案 我可能弄错了,但您没有指定Controller以及:action选项。您是否尝试过以下方法?假设您在路由中配置了一个messages资源:link_to'Reset',message_path(@message),:confirm=>'Areyousure?'编辑:以上已弃用。Rails4.0现在接受提示作为数据属性。请参

ruby - 设置Ruby中 float 的显示精度

在Ruby中可以设置float的显示精度吗?类似于:z=1/3z.to_s#=>0.33333333333333z.to_s(3)#=>0.333z.to_s(5)#=>0.33333或者我是否必须覆盖Float的to_s方法? 最佳答案 z.round(2)或x.round(3)是最简单的解决方案。参见http://www.ruby-doc.org/core-1.9.3/Float.html#method-i-round.也就是说,这只会确保它不超过那么多位数。在1/3的情况下没问题,但如果你说0.25.round(3)你会得到0

ruby 风格 : How to check whether a nested hash element exists

考虑存储在散列中的“人”。两个例子是:fred={:person=>{:name=>"Fred",:spouse=>"Wilma",:children=>{:child=>{:name=>"Pebbles"}}}}slate={:person=>{:name=>"Mr.Slate",:spouse=>"Mrs.Slate"}}如果“person”没有任何child,则“children”元素不存在。所以,对于Slate先生,我们可以检查他是否有parent:slate_has_children=!slate[:person][:children].nil?那么,如果我们不知道“slat

ruby-on-rails - "bin/rails: No such file or directory"w/Heroku 上的 Ruby 2 和 Rails 4

同时遵循MichaelHartl的Rails4Beta版本RubyonRailsTutorial,我的应用程序无法在Heroku上启动,但可以在本地使用bundleexecrailsserver正常运行。检查herokulogs-t显示以下错误:$heroku[web.1]:Statechangedfromcrashedtostarting$heroku[web.1]:Startingprocesswithcommand`bin/railsserver-p33847-e$RAILS_ENV`$app[web.1]:bash:bin/rails:Nosuchfileordirectory

ruby-on-rails - 雾警告和 AWS : unable to load the 'unf' gem

自从我上次更新包后,rails控制台(railsserver、railsconsole、db:migrate等)中的每个操作都会引发警告:[fog][WARNING]Unabletoloadthe'unf'gem.YourAWSstringsmaynotbeproperlyencoded.我确定我没有更改application.rb文件中的AWS字符串中的任何内容:#AmazonS3credentialsENV["AWS_ACCESS_KEY_ID"]="AWS_ACCESS_KEY_ID"ENV["AWS_SECRET_ACCESS_KEY"]="AWS_SECRET_ACCESS_

ruby-on-rails - redirect_to 在 rails 中使用 POST

是否可以使用POST方法重定向?还是应该始终使用GET进行重定向?它的用途是在电子商务网站的订单流程的最后步骤中,将数据发送到支付处理器,而无需为用户引入额外的步骤。 最佳答案 POST请求无法重定向-它是partoftheHTTP/1.1protocol.您可以引入另一个包含要发布到支付处理器的表单数据的步骤,或者您可以从您的应用程序发送帖子(我在使用PROTX时所做的事情)。 关于ruby-on-rails-redirect_to在rails中使用POST,我们在StackOverf

ruby - 将 float 四舍五入到 ruby​​ 中最接近的整数

如果我有一个49.967的float并且我执行.to_i它将把它削减到49这对于我使用磁盘空间分析.967超过900mb的空间不会在显示中考虑。是否有将数字四舍五入到最接近的整数的函数,或者我必须像这样自己定义它:classFloatdefto_nearest_i(self+0.5).to_iendend这样我就可以做:>>5.44.to_nearest_i=>5>>5.54.to_nearest_i=>6 最佳答案 试试Float.round。irb(main):001:0>5.44.round=>5irb(main):002:0