Tag: sinatra

在heroku雪松堆栈上部署sinatra app(使用config.ru)

我正在尝试重构我的sinatra代码,将我的主文件分成单独的文件,使用此响应中的一些提示,并且我遇到了部署到heroku的麻烦。 以前我没有config.ru文件,只使用了我的Procfile ,它是: web: bundle exec ruby web.rb -p $PORT 根据这篇文章 。 从重构开始,我现在将我的Procfile更改为 web: bundle exec thin -R config.ru start -p $PORT 我的config.ru文件正在 root = ::File.dirname(__FILE__) require ::File.join( root, ‘web’ ) run MyApp.new 我的web.rb文件包含在类定义中 class MyApp < Sinatra::Application # … end 这适用于我的本地开发计算机,但是当我部署到heroku时,我得到了 2011-12-01T11:21:54+00:00 app[web.1]: bundler: command not found: thin 2011-12-01T11:21:54+00:00 app[web.1]: Install missing gem executables […]

Sinatra – response.set_cookie不起作用

我需要为我的Sinatra应用程序使用cookie。 如果我使用最简单的方法是有效的: response.set_cookie(‘my_cookie’, ‘value_of_cookie’) 但我需要一些选项,如域名和过期日期,所以我试试这个: response.set_cookie(“my_cookie”, {:value => ‘value_of_cookie’, :domain => myDomain, :path => myPath, :expires => Date.new}) 不起作用。 没有cookie。 我需要这么多…… 请帮忙……谢谢!

如何从Sinatra中转储HTTP请求?

有没有办法以应用程序接收数据的确切方式将所有传入请求转储到Sinatra应用程序? 也许是某种Rack中间件?

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

我正在编写一个API,它接收一个JSON有效负载作为请求主体。 为了获得它,我正在做这样的事情: post ‘/doSomething’ do request.body.rewind request_payload = JSON.parse request.body.read #do something with request_payload body request_payload[‘someKey’] end 有什么方法可以抽象出来,这样我就不需要为每条路线做这件事了? 我的一些路由比这更复杂,因此request.body将通过这种方法重新读取和重新分析每个路由,我想避免。 有没有办法让request_payload神奇地可用于路由? 像这样: post ‘/doSomething’ do #do something with request_payload, it’s already parsed and available body request_payload[‘someKey’] end

在Heroku上部署Sinatra应用程序

我有简单的Sinatra应用程序。 web.rb: require ‘sinatra’ get ‘/’ do “Hello” end 的Gemfile:* source :rubygems gem ‘sinatra’, ‘1.1.0’ gem ‘thin’, ‘1.2.7’ config.ru: require ‘./web’ run Sinatra::Application 但是当我在Heroku上部署我的应用程序时,我会在日志中收到错误: 2012-03-27T19:17:48+00:00 heroku[router]: Error H14 (No web processes running) -> GET furious-waterfall-6586.herokuapp.com/ dyno= queue= wait= service= status=503 bytes= 我该如何解决?

Sinatra和问号

我需要用Sinatra制作一些看起来像的方法: 访问http //:本地主机:1234 /添加string_to_add 但是,当我宣布它是这样的: get “/add?:string_to_add” do … end 它没有看到string_to_add参数。 我应该如何声明我的方法并使用此参数来使事情有效?

切片参数哈希特定值

摘要 给定Hash,基于要使用的键列表创建子集Hash的最有效方法是什么? h1 = { a:1, b:2, c:3 } # Given a hash… p foo( h1, :a, :c, :d ) # …create a method that… #=> { :a=>1, :c=>3, :d=>nil } # …returns specified keys… #=> { :a=>1, :c=>3 } # …or perhaps only keys that exist 细节 Sequel数据库工具包允许通过传入Hash来创建或更新模型实例: foo = Product.create( hash_of_column_values ) foo.update( […]

使用RVM使用sinatra app加载Active Record gem时出错

我为我开始的sinatra应用程序设置了一个项目级别的RVM gemset,它将连接到具有Active Record的本地数据库。 为了测试它我尝试运行以下测试应用程序: test.rb require ‘rubygems’ # may not be needed, depending on platform require ‘sinatra’ require ‘activerecord’ class Article “sqlite3”, :database => “hw.db” ) Test.first.content end (摘自这个问题的答案: 使用Sinatra时,与数据库通信的最佳方式是什么? ) 当我运行ruby -rubygems test.rb此错误: /Users/[user]/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’: cannot load such file — activerecord (LoadError) 我已经安装了Active Record gem,它显示在gem list , rvm current显示正确的gemset。 我是RVM的新手,我认为这与它没有正确的加载路径有关,但我觉得我已经正确设置了所有内容,所以我很欣赏有关错误的建议。 谢谢。

Sinatra并不是以twitter gem开头的

当我尝试启动sinatra时,我遇到了以下错误 /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1488:in start_server’: undefined method用于HTTP的start_server’: undefined method运行:模块(NoMethodError) require ‘sinatra/base’ require_relative “twt.rb” class SinatraApp < Sinatra::Base set :static, true set :public_folder, File.dirname(__FILE__) + '/static' get '/getuserinfo' do @user = twit.getuserinfo erb :userInfo end end SinatraApp.run! 在“twt.rb”我需要推特(5.7.1) require ‘twitter’ class Twit attr_accessor :client def initialize(consumer_key,consumer_secret,access_token,access_token_secret) @client = Twitter::REST::Client.new do |config| config.consumer_key = consumer_key config.consumer_secret = consumer_secret config.access_token […]

ActiveRecord连接警告。 (数据库连接不会自动关闭)

我正在尝试用Sinatra和ActiveRecord创建一个小应用程序(3.2.3)。 这是我的主文件的样子: require “sinatra” require “sinatra/reloader” require “active_record” … ActiveRecord::Base.establish_connection( adapter: ‘sqlite3’, database: ‘db.sqlite3’, host: ‘localhost’, ) class Post < ActiveRecord::Base … end get('/') { … } get('/posts') { … } … 它有效,但有时我会在控制台中收到警告: 弃用警告:数据库连接不会自动关闭,请通过在连接上调用close线程末尾的数据库连接。 例如:ActiveRecord :: Base.connection.close’ 发生警告时,页面刷新需要很长时间。 我不明白我应该在哪里关闭连接。 我试图将ActiveRecord::Base.connection.close放在文件的底部,但它没有帮助。 更新: 我忘了提到我也使用来自sinatra-contrib gem的sinatra / reloader插件来查看效果而无需重启服务器。 require “sinatra/reloader” 如果我发表评论,那么问题就会消失。 但无论如何,我想知道如何在不禁用重新加载器的情况下解决问题。