Not sure if many people are familiar with Scaffolding Extensions for Ruby, but I've looked through their docs, forums, and even the source code of the Heroku test site, and not found an answer.
I made a basic Sinatra app and followed right from the RDoc's instructions:
require 'scaffolding_extensions'
require 'sinatra/base'
class Thingy < Sinatra::Base
scaffold_all_models
end
gives
undefined method 'scaffold_all_models' for Thingy:Class (NoMethodError)
and I know Scaffolding Extensions is loadable because I can successfully set some config variables in its classes.
This works for me:
require 'sinatra/base'
require 'scaffolding_extensions'
class Thingy < Sinatra::Base
scaffold_all_models
end
Related
The Sinatra docs say that development? will return true when the environment is development, but I receive an error stating that the method development? is undefined.
I tried skipping the shorthand and testing the ENV['RAKE_ENV'] variable itself, but it was just nil.
This is the error I'm getting:
undefined method `development?' for main:Object (NoMethodError)
and this is the code that is triggering the error:
require 'dm-sqlite-adapter' if development?
I am using the modular style app. The line above is an separate file that only manages the model. What's going on?
I struggled with this one, too. Here's what I discovered along the way.
You need to be "inside" a class that inherits from Sinatra::Base (such as Sinatra::Application, which inherits from Base) to be able to use the development? method, which is defined in base.rb.
In a classic Sinatra app you are already coding "inside" a class that inherits from Sinatra::Base. So development? will just work "anywhere".
In modular Sinatra the development? will only work in Sinatra::Base sub-classes, such as:
require 'sinatra/base'
# Placing
# require 'dm-sqlite-adapter' if development?
# here will not work.
class ApplicationController < Sinatra::Base
require 'dm-sqlite-adapter' if development? # But here it works
...
end
# Placing
# require 'dm-sqlite-adapter' if development?`
# AFTER the above class will still not work
class SomethingElse
# nor will `development?` work here, since it is called inside
# a class without Sinatra::Base inheritance
...
end
So basically you can use an ApplicationController class that inherits from Sinatra::Base and inside here check for development?. The same goes for sub-classes that inherits from your ApplicationController class:
class UserController < ApplicationController
require 'dotenv' if development?
...
end
For modular Sinatra, in the (main:Object) code text "outside" Sinatra::Base sub-classes, you need to follow Arup's instructions:
if Sinatra::Base.environment == :development
require 'awesome_print'
require 'dotenv'
Dotenv.load
...
end
Since you are using the modular style, you need to add the module namespace Sinatra::Base before the method.
So you will be able to access Sinatra::Base.development? anywhere in the app.
Any idea why Webrick refuses to start?
require 'sinatra/base'
require 'slim'
class Blog < Sinatra::Base
get '/' do
slim :home
end
end
Running ruby blog.rb does nothing. No error is raised.
The built in web server isn’t started when using the modular style of Sinatra apps. See the docs for the differences between modular and classic styles.
To get it to run like a classic style app, add this line to the bottom of your Blog class:
run! if app_file == $0
ok so this is very strange (well is to me), everything in my master branch works fine, I then created a new branch called twitter to conduct some twitter feed implementation. I have done this and was working yesterday on my linux machine.. I have pulled the branch today in a windows environment but when i load the app i now get the regular Sinatra 404 Sinatra doesn’t know this ditty.
This is my profile.rb file
require 'bundler/setup'
Bundler.require(:default)
require 'rubygems'
require 'sinatra'
require './config/config.rb' if File.exists?('./config/config.rb')
require 'sinatra/jsonp'
require 'twitter'
require 'sinatra/static_assets'
class Profile < Sinatra::Base
helpers Sinatra::Jsonp
enable :json_pretty
register Sinatra::StaticAssets
##twitter_client = Twitter::Client.new(
:consumer_key => ENV["CONSUMER_KEY"],
:consumer_secret => ENV["CONSUMER_SECRET"],
:oauth_token => ENV["OAUTH_TOKEN"],
:oauth_token_secret => ENV["OAUTH_SECRET"],
)
get '/' do
erb :index
end
get '/feed' do
jsonp ##twitter_client.user_timeline('richl14').map(&:attrs)
end
end
Config.ru
require './profile'
run Profile
Does anyone have any ideas of what i need to be looking at to solve this? Can anyone speak from experience with this?
Thanks
When you use the classic Sinatra style you use require 'sinatra' and then add routes to the top level. These routes get added to the Sinatra::Application. When you directly run this file, e.g. with ruby my_app.rb, Sinatra runs a built in web server, which will serve the Sinatra::Application app.
When you use the modular style, you use require 'sinatra/base', and then add routes to your Sinatra::Base subclass. In this case directly executing the file doesn’t start the built in server.
In your case you are using the modular style, but have used require 'sinatra'. You create your Profile app, but when you run the file directly Sinatra launches the built in server and serves the Sinatra::Application app. Since you haven’t added any routes to this (they’ve all been added to Profile) it runs but all requests return 404.
One way to get your app to launch you is to use rackup. This will launch the Profile app that you have explicitly set in your config.ru. (Explicitly starting your webserver will also work, e.g. using thin start).
Another possibility would be to add a line like this to the end of your Profile class:
run! if app_file == $0
This tells Sinatra to start the build in server running the Profile app if the file is the same as the Ruby file being executed, in a similar way to how the classic style app is launched. If you use this method you should change require 'sinatra' to require 'sinatra/base' otherwise you will get two servers launched, one after the other (in fact you should probably make that change anyway).
See the Sinatra docs for more info about the difference between classic and modular style.
I'm trying to use the url_for extension so my app can accurately find my static assets (tried the static assets extension as well, but it also complained of this same problem).
The problem is this:
undefined method `url_for' for Sinatra::Raffler:Class (NoMethodError)
Now, I've got the required modules listed, as per the url_for README:
require 'rubygems'
require 'sinatra/base'
require 'data_mapper'
require 'lib/authorization'
require 'pony'
gem 'emk-sinatra-url-for'
require 'sinatra/url_for'
But I'm still getting the NoMethodError when I try to call url_for
I have tried a couple of different things in regards to helpers. First, I have a helpers block for an authorization extension:
helpers do
include Sinatra::Authorization
end
So, I thought I could include the url_for helper in there:
helpers do
include Sinatra::Authorization
include Sinatra::UrlForHelper
end
But that didn't resolve the issue, so I just added the line:
helpers Sinatra::UrlForHelper
after that initial helpers do block, but still no resolution.
If you are subclassing Sinatra::Base, you need to include the helpers explicitly:
class Foobar < Sinatra::Base
helpers Sinatra::UrlForHelper
end
I have a gem that provides some rack middleware, the only way I can get it to work is to place this in my application.rb
config.middleware.use "TBBC::Editor::Middleware"
How can I make it so that this middleware is automatically used when my gem is used in an apps Gemfile?
If you intend your gem to be used with Rails 3, you could provide a Railtie. You can then automatically load it in case Rails is used.
Assuming your gem's name is tbbc, place this in lib/tbbc/railtie.rb:
module TBBC
class Railtie < Rails::Railtie
initializer "tbbc.insert_middleware" do |app|
app.config.middleware.use "TBBC::Editor::Middleware"
end
end
end
In lib/tbbc.rb:
require "tbbc/railtie" if defined? Rails
You can't automatically add the middleware to generic Rack apps. For non-Rails applications, this will be something the user has to do.