草庐IT

lumen-getting-current-url-paramet

全部标签

ruby-on-rails - 如何在 Rails 测试中轻松解析带参数的 URL?

我有一些代码将return_toURL嵌入到我要测试的重定向(如OpenID)中:deftest_uses_referrer_for_return_toexpected_return_to='http://test.com/foo'@request.env['HTTP_REFERER']=expected_return_toget:fazbot#@response.redirected_tolookslikehttp://service.com?...&return_to=[URI-encodedversionofURLabove]&...encoded_return_to=(some

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 - 如何从文本中提取 URL

如何从Ruby中的纯文本文件中提取所有URL?我尝试了一些库,但在某些情况下它们会失败。什么是最好的方法? 最佳答案 如果您喜欢使用Ruby中已经为您提供的功能:require"uri"URI.extract("textherehttp://foo.example.org/blaandheremailto:test@example.comandherealso.")#=>["http://foo.example.org/bla","mailto:test@example.com"]阅读更多:http://railsapi.com/d

ruby - Ruby 中 const_get 的混淆行为?

根据文档mod.const_get(sym)“返回mod中命名常量的值。”我也知道const_get默认情况下可能会查找接收者的继承链。所以以下工作:classA;HELLO=:hello;endclassB:hello我也知道Ruby中的类是Object的子类,因此您可以使用const_get来查找“全局”常量,即使接收方是一个普通类:classC;endC.const_get(:Array)#=>Array然而,这就是我感到困惑的地方——模块不继承Object。那么,为什么我仍然可以使用const_get从模块中查找“全局”常量?为什么以下方法有效?moduleM;endM.con

ruby-on-rails - 为什么 RSpec 的方法 "get"、 "post"、 "put"、 "delete"在 gem(或 Rails 外部)的 Controller 规范中不起作用?

我不是Rails或Rspec的新手,但我是制作gems的新手。当我测试我的Controller时,REST方法“get”、“post”、“put”、“delete”给我一个未定义的方法错误。您将在下面找到代码,但如果您更喜欢在馅饼中查看它,clickhere.谢谢!这是我的spec_helper:$LOAD_PATH.unshift(File.dirname(__FILE__))$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),'..','lib'))require'rubygems'require'active_support'u

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 - 如何在 rspec 功能测试中访问(设计)current_user?

在设计文档中,他们提供了有关在测试Controller时如何访问current_user的提示:https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29但是,在进行功能测试时呢?我正在尝试测试我的一个Controller的创建方法,并且在该Controller中使用了current_user变量。问题是devise中建议的宏使用了@request变量,并且对于功能规范来说它是nil。什么是解决方法?编辑:这是我目前的规范:feature

ruby - 如何获取 Sinatra 应用程序中的所有 URL 参数

使用以下Sinatra应用get'/app'docontent_type:json{"params"=>params}.to_jsonend调用:/app?param1=one¶m2=two¶m2=alt给出以下结果:{"params":{"param1":"one","param2":"alt"}}Params只有两个键,param1和param2。我知道Sinatra将参数设置为散列,但它并不代表所有的URL请求。在Sinatra中有没有办法获取请求中发送的所有URL参数的列表? 最佳答案 机架中的任何请求get

ruby - 从 URL 下载图像?

我正在尝试使用HTTP::get从我创建的URL下载Google图表的图像。这是我的第一次尝试:failures_url=[title,type,data,size,colors,labels].join("&")require'net/http'Net::HTTP.start("http://chart.googleapis.com"){|http|resp=http.get("/chart?#{failures_url")open("pie.png","wb"){|file|file.write(resp.body)}}只生成一个空的PNG文件。第二次尝试时,我在http.get(

ruby - RSpec stub : Return the parameter

虽然我的问题很简单,但我没能在这里找到答案:如何stub方法并返回参数本身(例如在执行数组操作的方法上)?像这样:interface.stub!(:get_trace).with().and_return() 最佳答案 注意:stub方法已被弃用。请看thisanswer以现代方式做到这一点。stub!可以接受一个block。block接收参数;block的返回值是stub的返回值:classInterfaceenddescribeInterfacedoit"shouldhaveastubthatreturnsitsargument