Skip to content

Templates are loaded from gem path by default? #40

igrigorik opened this Issue Apr 30, 2011 · 3 comments

3 participants

postrank-labs member
class API < Goliath::API
  include Goliath::Rack::Templates

  def response(env)
     # ...
     [200, {}, erb(:hello)]

I expected that to load the hello.erb file from views directory (same behavior as sinatra). Hitting the API with a request, shows:

{"error":"Template stream not found in /Users/igrigorik/.rvm/gems/ruby-1.9.2-p136/gems/goliath-0.9.1/lib/goliath/rack/views for erb"}%

It looks like I need to use Rack::Static before the template gets loaded from right place?

use(Rack::Static,:root => Goliath::Application.root_path("public"), :urls => ["/favicon.ico", '/stylesheets', '/javascripts', '/images'])

Seems like another gotcha, similar to ValidationError stuff which just got fixed... I would expect that the template should be loaded relative to my location by default and sourced from views.

postrank-labs member
mrflip commented Apr 30, 2011

Sigh -- I totally forgot to bring this up.

The Goliath::Application.app_file method, and thus the root_path method, is hinky:

    # Find the app_file that was used to execute the application
    # @return [String] The app file
    def self.app_file
      c = caller_files.first
      c = $0 if !c || c.empty?

    def self.root_path *args
      @root_path ||= File.expand_path(File.dirname(app_file))
      File.join(@root_path, *args)

if invoked from the wrong place, it will make Goliath confused where it lives. I think the reason including Rack::Static fixed it was simply by invoking root_path in the right place.

I don't understand enough about the caller_locations unwinding etc. to be able to write that method correctly. This is especially insidious inside a config file, since it is eval'ed.

postrank-labs member
dj2 commented May 1, 2011

Can you give an example where app_file and root_path don't work? I haven't seen any issues with the current versions.

@dj2 dj2 added a commit that closed this issue May 1, 2011
@dj2 dj2 root_path doesn't always do the right thing.
This fix is a bit of a hack. If we haven't exeuted the runner (ie, we're parsing the file)
then we haven't chdir'd to the API directory, which means we need a different meaning for
root_path. This fix adds an app_path method which can be used prior to the chdir. Root_path
can be used after the chdir. Like I said, hack. Can't think of a better way to handle
this at the moment without storing a global specifying if we've changed directories or not.

Fixes #40.
@dj2 dj2 closed this in 8738469 May 1, 2011
postrank-labs member
dj2 commented May 1, 2011

The example given should now work correctly. The templates.rb example works with and without the Rack::Static call now. The fix is a hack, let me know if you know a better way to handle the issue as described in the commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.