jjzjj

ruby-on-rails - 我如何在我的 RSpec 中设置 request.referrer?

我试图避免使用我的session[:referred_by],而想使用request.referrer。但是,我的RSpec测试失败了,因为TestRequest没有存储request.referrer因此,为了使Rspec测试正常工作,我必须执行以下操作。有没有办法让它变得更好:referrer=request.referrer?request.referrer:'/'redirect_toreferrer,:alert=>error_message 最佳答案 ActionDispatch::TestRequest扩展了Actio

ruby - 如何在 Sinatra 中只解析一次 JSON 请求体并将其暴露给所有路由?

我正在编写一个API,它接收一个JSON负载作为请求正文。为了得到它目前,我正在做这样的事情:post'/doSomething'dorequest.body.rewindrequest_payload=JSON.parserequest.body.read#dosomethingwithrequest_payloadbodyrequest_payload['someKey']end有什么好的方法可以将其抽象出来,这样我就不需要为每条路线都这样做了?我的一些路由比这更复杂,因此使用这种方法,request.body将在每条路由中被重新读取和重新解析多次,我想避免这种情况。有没有办法让r

ruby - 如何在 OpenURI 中指定 "http request header"

我正在尝试使用Ruby的OpenURIgem调用URL,但是它需要我在其HTTP请求header中传递某些值。知道怎么做吗? 最佳答案 根据thedocumentation,您可以将httpheader的哈希值作为第二个参数传递给open:open("http://www.ruby-lang.org/en/","User-Agent"=>"Ruby/#{RUBY_VERSION}","From"=>"foo@bar.invalid","Referer"=>"http://www.ruby-lang.org/"){|f|#...}

ruby - Rack::Request - 如何获取所有 header ?

标题是不言自明的。有什么方法可以获取header(Rack::Request.env[]除外)? 最佳答案 HTTPheader在Rackenvironment中可用。传递给您的应用:HTTP_Variables:Variablescorrespondingtotheclient-suppliedHTTPrequestheaders(i.e.,variableswhosenamesbeginwithHTTP_).Thepresenceorabsenceofthesevariablesshouldcorrespondwiththepr

ruby-on-rails - 没有参数的 request.fullpath

request.fullpath给出完整路径加上传递的任何参数,例如/page?param1=value¶m2&value如何只获取没有参数的页面?喜欢/page谢谢。 最佳答案 使用路径代替完整路径。虽然没有记录,request.path对我有用。我通常使用我的插件rails_web_console来处理请求对象。更新:正如turboladen注意到的那样,"[ActionDispatch::Request][2]继承自[Rack::Request‌][3],其中request.path为定义”。path没有记录在那里,但

ruby - 如何在 ruby​​ 中通过 SSL 调用 HTTP POST 方法?

所以这是使用curl的请求:curl-XPOST-Hcontent-type:application/json-d"{\"credentials\":{\"username\":\"username\",\"key\":\"key\"}}"https://auth.api.rackspacecloud.com/v1.1/auth我一直在尝试使用ruby​​发出同样的请求,但我似乎无法让它工作。我也尝试了几个库,但我无法让它工作。这是我到目前为止所拥有的:uri=URI.parse("https://auth.api.rackspacecloud.com")http=Net::HTTP.

ruby-on-rails - 使用 oauth 和 twitter ruby​​ gems 时不断出现 OAuth::Unauthorized 错误

我正在使用ruby​​twittergem和oauth来访问用户的twitter帐户。在我的代码中,我有:unless@user.twitter_authd?oauth=Twitter::OAuth.new('token','secret')session[:twitter_request_token]=oauth.request_token.tokensession[:twitter_request_secret]=oauth.request_token.secret@twitter_auth_url=oauth.request_token.authorize_urlendtoken

ruby-on-rails - 如何设置 ActionMailer default_url_options 的 :host dynamically to the request's hostname?

我正在尝试设置:hostforactionmailer默认url选项。我在所有环境文件中设置了以下内容config.action_mailer.default_url_options={:host=>"localhost"}我想通过提供请求主机使其更具动态性。当我尝试通过设置它时config.action_mailer.default_url_options={:host=>request.domain}或config.action_mailer.default_url_options={:host=>request.env["SERVER_NAME"]}它抛出错误...无法识别“请求

ruby-on-rails - 将 POST 数据从 Controller 提交到 Rails 中的另一个网站

用户提交包含一些基本数据的表单。数据由Controller中的操作接收和处理,并添加更多需要保密的信息。然后我需要向外部网站发送一个post请求,其中包含来自Controller的所有组合数据。执行此操作的最佳方法是什么? 最佳答案 最简单的方法是使用ruby​​核心库:require"uri"require"net/http"params={'box1'=>'Nothingislessimportantthanwhichforkyouuse.Etiquetteisthescienceofliving.Itembracesevery

ruby-on-rails - Rails 中的 request.remote_ip 和 request.ip 有什么区别?

如题所示,两种方法都可以获取到客户端的ip。我想知道是否有任何差异。谢谢。源码里有"/usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.3/lib/action_dispatch/http/request.rb"257L,8741Cdefip@ip||=superend#OriginatingIPaddress,usuallysetbytheRemoteIpmiddleware.defremote_ip@remote_ip||=(@env["action_dispatch.remote_ip"]||ip).to_send但我真