Environment configuration for Sinatra error - ruby

I have an environment config file at config/environment.rb with the following:
require 'rubygems'
require 'bundler'
Bundler.setup
require 'sinatra'
require 'sinatra/base'
require 'sinatra/reloader'
and in my config.ru I have:
require File.expand_path('../config/environment', __FILE__)
require 'slim'
require 'coffee-script'
require 'padrino-helpers'
require 'sinatra/twitter-bootstrap'
I am getting the error :
Errno::ENOENT at /profile
No such file or directory - /Users/myusername/projects/accounts/config/views/profile.slim
and this only goes away when I remove require 'sinatra' from the config/environment.rb file and into config.ru. Can anyone explain why this happens? I assumed that the require File.expand_path('../config/environment', __FILE__) will simply include all requires from that file into config.ru but that doesn't seem to be the case. It now thinks my views live inside the config folder.
I followed the suggestion given here: How do I make Rake tasks run under my Sinantra app/environment? but again, moving require 'sinatra' into the environment breaks the app.

The error looks like it is from Sinatra being unable to find a Slim template when rendering a response, because it’s looking for a views directory under the config directory. By default Sinatra looks for the views dir relative to the application file, which (again by default) is the file that calls require 'sinatra'. In your case the require line is in config/environment.rb so Sinatra treats that as the app file and looks for the views dir below it.
I’m assuming you have an actual application file that you haven’t shown. The simplest solution is probably to explicitly set the application file setting in there:
set :app_file, __FILE__
Depending on your setup you might want to specify the view directory directly instead:
set :views, 'path/to/views'

Related

Sinatra Routing - Separate Files

I'm going through a recently released book on Sinatra that demonstrates this way of setting up routes in different files:
# app.rb
require "sinatra"
require "slim"
class Todo < Sinatra::Base
# ...
Dir[File.join(File.dirname(__FILE__), "lib", "*.rb")].each { |lib| require lib }
end
# lib/routes.rb
get "/test" do
"The application is running"
end
# config.ru
require "sinatra"
require "bundler/setup"
Bundler.require
ENV["RACK_ENV"] = "development"
require File.join(File.dirname(__FILE__), "app.rb")
Todo.start!
However, it fails to find the route at http://localhost:4567/test. It would make sense to me that this should work when I run ruby config.ru or bundle exec rackup -p 4567. But coming from Rails development where all this configuration is built-in, I don't have a complete understanding of how everything gets wired together. The server is running on that port and I get the Sinatra doesn't know this ditty 404 page. If I reopen the class as suggested by this SO answer, the /test route is found.
# lib/routes.rb
class Todo < Sinatra::Base
get "/test" do
"The application is running"
end
end
Is there something I'm missing about this suggested way to include routes without reopening the class?
Try ruby app.rb, it should work.
You'll need to restart the webserver to load routes that were added while it was running. Routes are loaded into memory when app.rb is invoked and Sinatra is launched. The route itself looks fine and it appears routes.rb is being imported successfully via Dir[File.join(File.dirname(__FILE__), "lib", "*.rb")].each { |lib| require lib }.
If you're running the server directly through terminal Ctrl+X, Ctrl+C should shut it down, then restart it via rackup config.ru* or ruby app.rb. You may confirm the route is recognized by making a get request through your browser to: http://127.0.0.1:4567/test.
For the rackup config.ru command to work, you can change config.ru to something like:
# config.ru
require './app'
run Sinatra::Application
This is just a deployment convenience.
Edit: #shaun, because Todo extends Sinatra::Base it's fine to use run Todo in your case.
The book suggested Todo.start! to run the application from the config.ru file, but the Sinatra documentation example uses run Sinatra::Application. So I just changed the line from Todo.start! to
run Todo
That seems to work, but I'll have to look into the consequences.

Do we need any database.yml file for Ruby on rails application using Redis

I have created a Ruby on Rails application without active records:-
$ rails new rails_using_redis_demo_app --skip-active-record
Now in order to perform database operation to i have to include database.yml file containing the necessary details regarding the database adapter.
I have also include follwing in my gemfile:-
gem 'redis'
I also found that redis actually stores the data in a .rdb file with key value pair, which can be accessed with the help of keys present. Am I doing things in a right manner.
Have you removed all occurences to ActiveRecord in your Rails config files ?
Perhaps you should check your application.rb file.
A typical application.rb file includes
require 'rails/all'
That is what you should write instead to fully remove ActiveRecord
# Pick the frameworks you want:
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
Hope that helps...

Why is my Sinatra app showing a blank screen?

My Sinatra app was working on my local server a few days ago, but now it simply isn't running, and all I get is a blank screen. Even when I deploy to Heroku, nothing works.
Here's a GitHub repository where you can check out my code: https://github.com/aayalur/Sinfoursq
I think the problem is with my config.ru file.
# Gemfile
#require "rubygems"
#require "bundler/setup"
#require "sinatra"
require "./main"
set :run, false
set :raise_errors, true
run Sinatra::Application
Thanks!
You app doesn't display anything because you haven't closed a script tag. You would have noticed that if you inspected the DOM.
By the way, you start your app twice: once in main.rb, and then in config.ru.
You can notice that when Ctrl+C'ing the app: it starts again and you gotta shut it down a second time.
Since your app needs to be run on Rack, get rid of the Sinatra.run! if __FILE__ == $0 and start it with rackup, as explained in Sinatra's documentation.
This will fix the problems related to running the app.
In your config.ru leave this way:
require "rubygems"
require "bundler"
Bundler.require
require "./app"
run Sinatra::Application

issues finding public folder when requiring sinatra/base

I have found that in my Sinatra app, when I require 'sinatra', I can access my public folder as expected, but when I require 'sinatra/base' I can't. Here is my relevant code (which works until I change to /base):
config.ru
root = ::File.dirname(__FILE__)
require ::File.join( root, 'app' )
run MyApp.new
app.rb
require 'sinatra'
require 'sinatra/namespace'
require 'haml'
class MyApp < Sinatra::Application
# ...
end
require_relative 'models/init'
require_relative 'helpers/init'
require_relative 'routes/init'
script.haml
%script(type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js")
%script(type="text/javascript" src="/js/table.js")
%link(rel="stylesheet" type="text/css" href="/css/table.css")
And yes, I have the correct directory structure in place. Like I said, it works using require sinatra. Anyone know why this is occurring and what I can do to fix it?
Requiring Sinatra::Base does not set any of the default configuration settings that requiring Sinatra does. You'll need to set :public_folder ... to a suitable value yourself, e.g:
set :public_folder, 'public'

Reloading Sinatra app on every request on Windows

I've set up Rack::Reload according to this thread
# config.ru
require 'rubygems'
require 'sinatra'
set :environment, :development
require 'app'
run Sinatra::Application
# app.rb
class Sinatra::Reloader < Rack::Reloader
def safe_load(file, mtime, stderr = $stderr)
if file == Sinatra::Application.app_file
::Sinatra::Application.reset!
stderr.puts "#{self.class}: reseting routes"
end
super
end
end
configure(:development) { use Sinatra::Reloader }
get '/' do
'foo'
end
Running with thin via thin start -R config.ru, but it only reloads newly added routes. When I change already existing route, it still runs the old code.
When I add new route, it correctly reloads it, so it is accessible, but it doesn't reload anything else.
For example, if I changed routes to
get '/' do
'bar'
end
get '/foo' do
'baz'
end
Than / would still serve foo, even though it has changed, but /foo would correctly reload and serve baz.
Is this normal behavior, or am I missing something? I'd expect whole source file to be reloaded. The only way around I can think of right now is restarting whole webserver when filesystem changes.
I'm running on Windows Vista x64, so I can't use shotgun because of fork().
You could try sinatra-reloader, which is known to work well on Windows (also, it's faster than shotgun).
This works:
# config.ru
require 'rubygems'
require 'app'
set :environment, :development
run Sinatra::Application
# app.rb
require 'sinatra'
class Sinatra::Reloader < Rack::Reloader
def safe_load(file, mtime, stderr = $stderr)
if file == File.expand_path(Sinatra::Application.app_file)
::Sinatra::Application.reset!
stderr.puts "#{self.class}: reseting routes"
end
super
end
end
configure(:development) { use Sinatra::Reloader }
get '/' do
'foo'
end
It matters from where you have the require statement. But I find the following solution more elegant and robust:
# config.ru
require 'rubygems'
require 'sinatra'
require 'rack/reloader'
require 'app'
set :environment, :development
use Rack::Reloader, 0 if development?
run Sinatra::Application
# app.rb
Sinatra::Application.reset!
get '/' do
'foo'
end
Does Shotgun not work on Windows?
From the README:
Shotgun
This is an automatic reloading version of the rackup command that's shipped with
Rack. It can be used as an alternative to the complex reloading logic provided
by web frameworks or in environments that don't support application reloading.
The shotgun command starts one of Rack's supported servers (e.g., mongrel, thin,
webrick) and listens for requests but does not load any part of the actual
application. Each time a request is received, it forks, loads the application in
the child process, processes the request, and exits the child process. The
result is clean, application-wide reloading of all source files and templates on
each request.
You can also try using Trinidad a JRuby Rack container based on Tomcat. In my experience it does change reloading by default without having to modify your source files. Bloody fast too. Obviously no good if you are using native libraries, but if you are deploying on Windows you are probably used to adopting a pure-ruby approach.
Its syntax is just as simple as the thin approach:
jruby -S trinidad -r config.ru
There is no Java specific yak shaving (i.e. creating web.xml or WARing up your Ruby app) and the gem is simple to install.

Resources