Sinatra/CouchDB 错误?

发布于 2024-09-06 22:47:30 字数 1142 浏览 2 评论 0原文

我正在开发我的第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×纯※雪 2024-09-13 22:47:31

经过大量谷歌搜索后,我发现 1.4 系列的 json.gem 已知会导致很多问题。我卸载了所有 json gem,并安装了 json-1.2.4.gem。我现在一切正常。这是我正在使用的设置:

config.ru(Rackup 文件):

require 'application'

set :environment, :production
set :root, File.dirname(__FILE__)
set :run, false

FileUtils.mkmdir_p 'log' unless File.exists?('log')
log = File.new('log/sinatra.log', 'a+')
$stdout.reopen(log)
$stderr.reopen(log)

run Sinatra::Application

environment.rb

require 'rubygems'
require 'couchrest'
require 'haml'
require 'ostruct'

require 'sinatra' unless defined?(Sinatra)

configure do
  SiteConfig = OpenStruct.new(
    :title => 'Application Title',
    :author => 'Your Name',
    :url_base => 'Your URL',
    :url_base_db => 'Your CouchDB Server',
    :db_name => "Your DB Name"
  )

  # load models
  $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
  Dir.glob("#{File.dirname(__FILE__)}/lib/*.rb") { |lib| require File.basename(lib, '.*') }
end

lib/contact.rb(模型示例,模型在 environment.rb 中自动加载):

class Contact < CouchRest::ExtendedDocument
  include CouchRest::Validation
  use_database CouchRest.database!((SiteConfig.url_base_db || '') + SiteConfig.db_name)

  property :name
  timestamps!

  view_by :name
end

application.rb

require 'rubygems'
require 'sinatra'
require 'environment'

configure do
  set :views, "./views"
end

error do
  e = request.env['sinatra.error']
  Kernel.puts e.backtrace.join("\n")
  'Application error'
end

helpers do
end

get '/new/?' do
  haml :new
end

post '/save/?' do
  @contact_name = params[:contact_name]

  @contact = Contact.new
  @contact.name = @contact_name
  @contact.save

  haml :save
end

get '/' do
  haml :index
end

希望这对将来的人有帮助!

After a lot of googling, I discovered that the 1.4 series of json.gem are known to cause a lot of problems. I uninstalled all the json gems I had and installed json-1.2.4.gem instead. I have everything working correctly now. Here's the setup I'm using:

config.ru (Rackup file):

require 'application'

set :environment, :production
set :root, File.dirname(__FILE__)
set :run, false

FileUtils.mkmdir_p 'log' unless File.exists?('log')
log = File.new('log/sinatra.log', 'a+')
$stdout.reopen(log)
$stderr.reopen(log)

run Sinatra::Application

environment.rb:

require 'rubygems'
require 'couchrest'
require 'haml'
require 'ostruct'

require 'sinatra' unless defined?(Sinatra)

configure do
  SiteConfig = OpenStruct.new(
    :title => 'Application Title',
    :author => 'Your Name',
    :url_base => 'Your URL',
    :url_base_db => 'Your CouchDB Server',
    :db_name => "Your DB Name"
  )

  # load models
  $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
  Dir.glob("#{File.dirname(__FILE__)}/lib/*.rb") { |lib| require File.basename(lib, '.*') }
end

lib/contact.rb (Model example, models auto-loaded in environment.rb):

class Contact < CouchRest::ExtendedDocument
  include CouchRest::Validation
  use_database CouchRest.database!((SiteConfig.url_base_db || '') + SiteConfig.db_name)

  property :name
  timestamps!

  view_by :name
end

application.rb:

require 'rubygems'
require 'sinatra'
require 'environment'

configure do
  set :views, "./views"
end

error do
  e = request.env['sinatra.error']
  Kernel.puts e.backtrace.join("\n")
  'Application error'
end

helpers do
end

get '/new/?' do
  haml :new
end

post '/save/?' do
  @contact_name = params[:contact_name]

  @contact = Contact.new
  @contact.name = @contact_name
  @contact.save

  haml :save
end

get '/' do
  haml :index
end

Hope this helps someone in the future!

要走就滚别墨迹 2024-09-13 22:47:31

尝试在设置 :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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文