Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `desc' for Sinatra::Application:Class

Tags:

ruby

rake

sinatra

This is the error I get when I run any rake command: 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'

Full trace:

/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 `block (2 levels) in delegate'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-activerecord-1.2.3/lib/sinatra/activerecord/tasks.rake:3:in `<top (required)>'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `load'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `block in load'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in `load_dependency'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `load'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-activerecord-1.2.3/lib/sinatra/activerecord/rake.rb:77:in `<top (required)>'
/Users/j/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in `require'
/Users/j/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in `require'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/backports-3.3.5/lib/backports/tools.rb:328:in `require_with_backports'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `block in require'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in `load_dependency'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `require'
/Users/j/Desktop/app/Rakefile:2:in `<top (required)>'
/Users/j/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/rake_module.rb:25:in `load'
/Users/j/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/rake_module.rb:25:in `load_rakefile'
/Users/j/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:637:in `raw_load_rakefile'
/Users/j/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:94:in `block in load_rakefile'
/Users/j/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/Users/j/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:93:in `load_rakefile'
/Users/j/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:77:in `block in run'
/Users/j/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/Users/j/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
/Users/j/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/bin/rake:33:in `<top (required)>'
/Users/j/.rvm/gems/ruby-2.0.0-p247@global/bin/rake:23:in `load'
/Users/j/.rvm/gems/ruby-2.0.0-p247@global/bin/rake:23:in `<main>'
/Users/j/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `eval'
/Users/j/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `<main>'
like image 801
jamesfzhang Avatar asked Dec 17 '13 15:12

jamesfzhang


2 Answers

Found an easiest solution:

Juste add the require:false attribute to sinatra contrib in your gemfile:

gem "sinatra-contrib",require: false

Found this here:

http://aaronlerch.github.io/blog/sinatra-bundler-and-the-global-namespace/

like image 50
tomsoft Avatar answered Oct 29 '22 15:10

tomsoft


The Sinatra namespace extension from Sinatra contrib is interfering with Rake’s own namespace support. They both define a namespace method, and the Sinatra contrib version is being called (incorrectly) from the Sinatra-ActiveRecord Rake tasks.

If you’re not using the namespaces from Sinatra-contrib, then the easiest solution would be to only require those extensions that you need; e.g. change

require 'sinatra/contrib'

to

require 'sinatra/whatever'
require 'sinatra/anotherextension'

If you are using Sinatra namespaces then I think you may be able to get round this by moving to a modular style app. Change your app.rb to something like

require 'sinatra/base' # note this has changed from just 'sinatra'
require 'sinatra/activerecord'
require 'sinatra/contrib'

class MyApp < Sinatra::Base
  register Sinatra::Contrib

  get '/' do
    "Hello World"
  end

  # other routes etc. as needed
end

Then in your config.ru you need run MyApp rather then run Sinatra::Application (of course you can – and should – give your class a better name). This avoids the collision of the two namespace methods, since the Sinatra version is only available in your application class, not the top level.

like image 24
matt Avatar answered Oct 29 '22 15:10

matt