Sinatra/CouchDB 错误?
我正在开发我的第一个 Sinatra/CouchDB 项目,但遇到了一个无法解释的错误。
这是我的rackup (config.ru
) 文件:
require 'rubygems'
require 'couchrest'
require 'patina'
set :environment, :development
set :root, File.dirname(__FILE__)
set :run, false
FileUtils.mkdir_p 'log' unless File.exists?('log')
log = File.new("log/sinatra.log", "a")
$stdout.reopen(log)
$stderr.reopen(log)
set :db, CouchRest.database!("http://127.0.0.1:5984/test")
run Sinatra::Application
这是应用程序文件(patina.rb
):
require 'rubygems'
require 'sinatra'
require 'couchrest'
require 'haml'
class Article < CouchRest::ExtendedDocument
use_database settings.db
property :title
timestamps!
view_by :title
end
get '/' do
@db = settings.db
haml :index
end
patina.rb
中没有类定义,该路由返回一个显示 @db
属性的页面,正如我所期望的那样。但是,当我将类定义添加到 patina.rb
时,我收到“Ruby (Rack) 应用程序无法启动”错误消息。
显然这与我的类定义有关,但我无法弄清楚问题是什么,并且错误消息似乎对我没有帮助。
另外,我实际上更喜欢将类定义放在单独的文件 (Article.rb
) 中,但我不知道如何在我的 Sinatra 应用程序的上下文中执行此操作。
任何帮助将不胜感激!
编辑:
请参阅下面我的答案。
I'm working on my first Sinatra/CouchDB project and I'm getting an error I can't explain.
Here's my rackup (config.ru
) file:
require 'rubygems'
require 'couchrest'
require 'patina'
set :environment, :development
set :root, File.dirname(__FILE__)
set :run, false
FileUtils.mkdir_p 'log' unless File.exists?('log')
log = File.new("log/sinatra.log", "a")
$stdout.reopen(log)
$stderr.reopen(log)
set :db, CouchRest.database!("http://127.0.0.1:5984/test")
run Sinatra::Application
And here's the app file (patina.rb
):
require 'rubygems'
require 'sinatra'
require 'couchrest'
require 'haml'
class Article < CouchRest::ExtendedDocument
use_database settings.db
property :title
timestamps!
view_by :title
end
get '/' do
@db = settings.db
haml :index
end
Without the class definition in patina.rb
, the route returns a page that displays the @db
property as I was expecting. However, when I add the class definition to patina.rb
I get "Ruby (Rack) application could not be started" error message.
Obviously this has something to do with my class definition, but I can't figure out what the problem is and the error message doesn't seem that helpful to me.
Also, I'd actually prefer to have the class definition in a separate file (Article.rb
), but I can't figure out how to do that in the context of my Sinatra app.
Any help would be greatly appreciated!
EDIT:
See my answer below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过大量谷歌搜索后,我发现
1.4
系列的json.gem
已知会导致很多问题。我卸载了所有 json gem,并安装了 json-1.2.4.gem。我现在一切正常。这是我正在使用的设置:config.ru
(Rackup 文件):environment.rb
:lib/contact.rb
(模型示例,模型在environment.rb
中自动加载):application.rb
:希望这对将来的人有帮助!
After a lot of googling, I discovered that the
1.4
series ofjson.gem
are known to cause a lot of problems. I uninstalled all the json gems I had and installedjson-1.2.4.gem
instead. I have everything working correctly now. Here's the setup I'm using:config.ru
(Rackup file):environment.rb
:lib/contact.rb
(Model example, models auto-loaded inenvironment.rb
):application.rb
:Hope this helps someone in the future!
尝试在设置 :db 后要求“patina”。我认为 Article 的类主体在设置存在之前正在执行 use_database 方法。
你应该能够将 Article 放在article.rb 中(Ruby 命名约定对于类来说是 UpperCamel,但是对于定义类的文件来说是 under_scores),然后在 patina.rb 中 require 'article' 。
这是对我来说唯一突出的事情,所以请告诉我这是否有效。
try requiring 'patina' after setting :db. I think the class body of Article is executing the use_database method before the setting exists.
you should be able to put Article in article.rb (ruby naming convention is UpperCamel for classes, but under_scores for the files in which classes are defined) and then require 'article' in patina.rb.
thats the only thing that stood out for me, so let me know if that works.