Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra cannot find views on Ruby 1.9.2-p0

I'm quite new to Ruby language (up to now I developed in Groovy + Grails) but since I was curious about it I wanted to try Sinatra on Ruby 1.9.2-p0.

I have a trivial website that is contained in /mywebpage and has 2 files:

# blog.rb
get '/' do
  'Hello World!'
end

get '/impossible' do
  haml :index
end

and

#config.ru
path = File.expand_path "../", __FILE__

$LOAD_PATH << (File.expand_path ".") + "/views"

require 'haml'
require 'sinatra'
require "#{path}/blog"

run Sinatra::Application

then in the same folder I have a /views/ folder that contains index.haml.

I try to run the server with rackup -p8080 but when I try to get /impossible I receive the following error:

Errno::ENOENT at /impossible
No such file or directory - /home/jack/mywebpage/<internal:lib/rubygems/views/index.haml

By searching over internet it seems that this maybe caused by "." not being included in $LOAD_PATH so I tried to add it or add directly views ./views so that actually $LOAD_PATH.inspect gives me correct path: ..., "/home/jack/mywebpage/views"]

But still it doesn't seem to work. Being quite new to the framework and the language I was wondering if I'm doing something wrong. any clues?

like image 501
Jack Avatar asked Sep 18 '10 16:09

Jack


3 Answers

Running Sinatra with Ruby 1.9.2 the template directory is no longer implicitly 'views', you need to set it yourself.

set :views, File.dirname(__FILE__) + "/views"

Note that currently Ruby also has Kernel#__dir__() method that is equivalent to File.dirname(__FILE__).

like image 100
rengo.Java Avatar answered Nov 05 '22 15:11

rengo.Java


gem install sinatra --pre
like image 44
Konstantin Haase Avatar answered Nov 05 '22 15:11

Konstantin Haase


I ran into a similar problem, and solved it like this. I didn't dig into the problem, but this is what I found and it works. It'll supposedly be fixed in the next version of Sinatra (which they should really get out the door, just to fix these few 1.9.2 bugs).

#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'

enable :run

get '/' do
  "Hello, world!"
end

Edit: It seems there are multiple bugs with Sinatra on 1.9.2. This one will fix Sinatra apps not starting on 1.9.2. I don't use a views directory (I like to keep my apps single-file), so I didn't run into your particular problem. This fix most likely won't help you at all. I probably should have read your problem more closely..

like image 1
AboutRuby Avatar answered Nov 05 '22 15:11

AboutRuby