草庐IT

Get-Volume

全部标签

ruby - 导轨 : how to get a file extension/postfix based on the mime type

我的问题是,RubyonRails是否具有类似于以下的功能:file_content_type=MIME::Types.type_for(file).first.content_type这将返回特定mime类型的文件扩展名或后缀?所以如果我传入'image/jpeg'函数将返回'jpg'寻找一种比编写完成相同工作的case语句更简洁的编码方式。 最佳答案 Rack::Mime具有这种能力(Rack是Rails的依赖):require'rack/mime'Rack::Mime::MIME_TYPES.invert['image/jpe

ruby - 如何使用修改后的 header 制作 HTTP GET?

使用修改后的header在Ruby中发出HTTPGET请求的最佳方式是什么?我想从日志文件的末尾获取一系列字节,并一直在玩弄以下代码,但服务器返回一个响应说“这是服务器无法理解的请求”(服务器是Apache)。require'net/http'require'uri'#with@address,@port,@pathalldefinedelsewherehttpcall=Net::HTTP.new(@address,@port)headers={'Range'=>'bytes=1000-'}resp,data=httpcall.get2(@path,headers)有没有更好的方法在R

ruby - 为什么 rubocop 或 ruby​​ 风格指南不喜欢使用 get_ 或 set_?

我在我的项目上运行rubocop并修复它提出的投诉。一个特别的提示困扰着我Donotprefixreadermethodnameswithget_我无法从这个投诉中了解太多,所以我查看了sourcecodeingithub.我找到了这个片段defbad_reader_name?(method_name,args)method_name.start_with?('get_')&&args.to_a.empty?enddefbad_writer_name?(method_name,args)method_name.start_with?('set_')&&args.to_a.one?end

ruby-on-rails - rails : How to get has_many associations of a model

如何获取模型的has_many关联?例如,如果我有这个类:classA我想要这样的方法:A.get_has_many返回[B,C]这可能吗?谢谢! 最佳答案 您应该使用ActiveRecordreflections.然后你可以这样输入:A.reflect_on_all_associations.map{|assoc|assoc.name}这将返回你的数组[:B,:C] 关于ruby-on-rails-rails:Howtogethas_manyassociationsofamodel,我

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 - 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 - Rails : how to get the current host, 或用它构建 URL?

这个问题在这里已经有了答案:GettingtheHostnameorIPinRubyonRails(12个答案)关闭8年前。我需要使用主机构建具有不同端口的URL。例如,如果主机是example.com,我需要生成类似http://example.com:8080/的URL我需要它是便携的,所以当我在本地环境中时,它会显示http://localhost:8080/相反。有什么想法吗?

ruby-on-rails - RSpec 测试使用 GET 参数重定向到 URL

假设我有一个带有redirect_to_baz方法的FoosController。classFoosController我正在使用spec/controllers/foos_controller_spec.rb对此进行测试:require'spec_helper'describeFoosController,:type=>:controllerdodescribe"GETredirect_to_baz"doit"redirectstoexample.comwithparams"doget:redirect_to_bazexpect(response).toredirect_to"htt

ruby-on-rails - Ruby Net::HTTP::Get 和 JSON 响应

我正在尝试连接到API并使用我的Rails应用检索json结果,但它似乎不起作用。举个例子:@request=Net::HTTP::Get.new"http://example.com/?search=thing&format=json"当我在浏览器中尝试url时,它有效!我得到JSON数据,但是当我在Ruby中尝试时,正文为零。>>y@request---!ruby/object:Net::HTTP::Getbody:body_stream:header:accept:-"*/*"user-agent:-Rubymethod:GETpath:http://example.com/?s