我有一个简单的sinatra应用程序需要生成一个文件(通过外部进程),将该文件发送到浏览器,最后从文件系统中删除该文件.这些方面的东西:
class MyApp < Sinatra::Base
get '/generate-file' do
# calls out to an external process,
# and returns the path to the generated file
file_path = generate_the_file()
# send the file to the browser
send_file(file_path)
# remove the generated file, so we don't
# completely fill up the filesystem.
File.delete(file_path)
# File.delete is never called.
end
end
Run Code Online (Sandbox Code Playgroud)
但是,似乎send_file调用完成了请求,并且它之后的任何代码都没有运行.
有没有办法确保生成的文件在成功发送到浏览器后被清除?或者我是否需要在某个时间间隔内使用运行清理脚本的cron作业?
我需要为我的Sinatra应用程序使用cookie.如果我使用最简单的方法是有效的:
response.set_cookie('my_cookie', 'value_of_cookie')
Run Code Online (Sandbox Code Playgroud)
但我需要一些选项,如域名和过期日期,所以我试试这个:
response.set_cookie("my_cookie", {:value => 'value_of_cookie', :domain => myDomain, :path => myPath, :expires => Date.new})
Run Code Online (Sandbox Code Playgroud)
不起作用.没有cookie.我需要这么多......
请帮忙......谢谢!
新的Sinatra,只是开发服务器启动和运行,但rackup使用WEBrick而不是Thin,Thin已经安装了宝石,这必须是一个简单的配置调整,但我不知道在哪里.哦,当你在它,Thin当我更改源代码时自动刷新?WEBrick当我更改源代码时,似乎必须停止并重新启动.
编辑
正如所建议的那样,thin start对我的设置进行调整.它本身会抛出一个错误"start_tcp_server": no acceptor (RuntimeError),这意味着我已经在该端口上运行了另一个服务.要解决这个问题,我只需运行thin start -p 9292.希望这有助于其他人.
这是我的 Gemfile
source :rubygems
gem 'rake', '0.9.2.2'
gem 'sinatra'
gem 'activerecord', '3.0.9'
gem 'pg', '~> 0.12.2'
gem 'logger'
gem 'nokogiri'
group :development, :test do
gem 'rack-test'
gem 'ruby-debug19'
gem 'sqlite3'
end
Run Code Online (Sandbox Code Playgroud)
我rake console在其他项目中运行,现在我收到此消息:
You have already activated activesupport 3.1.3, but your Gemfile requires activesupport 3.0.9. Using bundle exec may solve this.
我如何使用`bundle exec来解决这个问题?这是什么意思?
在创建给定的ActiveRecord模型对象的实例时,我需要生成一个短的(6-8个字符)唯一字符串,用作URL中的标识符,采用Instagram的照片URL样式(如http://instagram.com/ p/P541i4ErdL /,我刚刚加入404)或Youtube的视频网址(如http://www.youtube.com/watch?v=oHg5SJYRHA0).
这样做的最佳方法是什么?最简单的方法是重复创建一个随机字符串,直到它是唯一的?有没有办法散列/随机播放整数id,以便用户不能通过更改一个字符来破解URL(就像我上面的404'd Instagram链接那样)并最终获得新记录?
这是我运行任何rake命令时得到的错误:undefined method 'desc' for Sinatra::Application:Class
# app.rb
require 'sinatra'
require 'sinatra/activerecord'
require 'sinatra/contrib'
get '/' do
puts "Hello World"
end
# config.ru
require "./app"
run Sinatra::Application
# Rakefile
require './app'
require 'sinatra/activerecord/rake'
# Gemfile
source 'https://rubygems.org'
ruby '2.0.0'
gem 'activerecord', '~> 4.0.2'
gem 'sinatra', '~> 1.4.4'
gem 'sinatra-activerecord', '~> 1.2.3'
gem 'sinatra-contrib', '~> 1.4.2'
Run Code Online (Sandbox Code Playgroud)
完整跟踪:
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-contrib-1.4.2/lib/sinatra/namespace.rb:269:in `method_missing'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-activerecord-1.2.3/lib/sinatra/activerecord/tasks.rake:4:in `block in <top (required)>'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-contrib-1.4.2/lib/sinatra/namespace.rb:126:in `class_eval'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-contrib-1.4.2/lib/sinatra/namespace.rb:126:in `block in new'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-contrib-1.4.2/lib/sinatra/namespace.rb:118:in `initialize'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-contrib-1.4.2/lib/sinatra/namespace.rb:118:in `new'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-contrib-1.4.2/lib/sinatra/namespace.rb:118:in `new'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-contrib-1.4.2/lib/sinatra/namespace.rb:142:in `namespace'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.4/lib/sinatra/base.rb:1972:in …Run Code Online (Sandbox Code Playgroud) 由于某种原因,我的应用程序中没有设置会话变量.我正在使用Sinatra 1.2.1.
这是一段代码:
module GitWiki
class App < Sinatra::Base
configure do
enable :sessions
set :app_file, __FILE__
set :root, File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
set :auth do |bool|
condition do
redirect '/login' unless logged_in?
end
end
end
helpers do
def logged_in?
not @user.nil?
end
end
error PageNotFound do
page = request.env["sinatra.error"].name
redirect "/#{page}/edit"
end
before do
content_type "text/html", :charset => "utf-8"
@user = session[:user]
end
get "/login/?" do
erb :login
end
post "/login" do
user = User.get
if user.authenticate(params[:username], params[:password])
session[:user] = params[:username] …Run Code Online (Sandbox Code Playgroud) 我正在编写一个调用一些外部服务的Sinatra应用程序.我希望我的测试显然可以避免调用真正的服务,所以在我拥有它的那一刻
class MyApp < Sinatra::Base
get '/my_method' do
@result = ExternalServiceHandler.new.do_request
haml :my_view
end
end
Run Code Online (Sandbox Code Playgroud)
在我的测试中
describe "my app" do
include Rack::Test::Methods
def app() MyApp end
it "should show OK if call to external service returned OK" do
@external_service_handler = MiniTest::Mock.new
@external_service_handler.expect :do_request, "OK"
#Do the injection
get '/my_method'
response.html.must_include "OK"
end
it "should show KO if call to external service returned KO" do
@external_service_handler = MiniTest::Mock.new
@external_service_handler.expect :do_request, "KO"
#Do the injection
get '/my_method'
response.html.must_include "KO"
end
end
Run Code Online (Sandbox Code Playgroud)
我可以想到两种注入方法.我可以调用实例方法或通过构造函数传递依赖项.无论如何,因为机架似乎没有让我访问当前的应用程序实例,我发现这是不可能的. …
我有一个Sinatra"hello world"应用程序,我试图使用jRuby运行.它在我运行应用程序时有效,但在我运行机架时则无效.谁能告诉我这里发生了什么?
这是应用程序,在'app.rb'文件中:
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
configure do
set :bind, '0.0.0.0'
end
get '/' do
'Boo!'
end
Run Code Online (Sandbox Code Playgroud)
我可以运行它bundle exec ruby app.rb,它工作正常:
jonea@centos7andy[~/andy/sinatra_sand_jruby]%: bundle exec ruby app.rb
[2015-01-12 10:36:06] INFO WEBrick 1.3.1
[2015-01-12 10:36:06] INFO ruby 1.9.3 (2014-12-09) [java]
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from WEBrick
[2015-01-12 10:36:06] INFO WEBrick::HTTPServer#start: pid=31654 port=4567
Run Code Online (Sandbox Code Playgroud)
这是我的config.ru来调用上面的程序:
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require './app'
run Sinatra::Application
Run Code Online (Sandbox Code Playgroud)
如果我运行它,它似乎工作,但我无法使用Web浏览器访问服务器:
jonea@centos7andy[~/andy/sinatra_sand_jruby]%: bundle exec rackup -p4567 …Run Code Online (Sandbox Code Playgroud)