1
votes

I am following the recipe found on this tutorial on Sinatra::Base https://www.safaribooksonline.com/library/view/sinatra-up-and/9781449306847/ch04.html

I am having an issue getting my routes to work, currently only one route works and that is get '/' which loads from ApplicationController < Sinatra::Base home.erb.

the route in my second controller which is named ExampleController < ApplicationController does not work

config.ru (relevant code only)

# Load Controllers and models
Dir.glob('./{controllers,models}/*.rb').each { |file| require file }

map('/example') { run ExampleController }
map('/') { run ApplicationController }

application_controller.rb

class ApplicationController < Sinatra::Base
  # set folder for root
  set :root, File.expand_path("../app", Dir.pwd)

  # don't enable logging when running tests
  configure :production, :development do
    enable :logging
  end

  get '/' do
    title "Home.erb"
    erb :home
  end

  not_found do
    title 'Not Found!'
    erb :not_found
  end

end

example_controller.rb which the routes will not load from currently

class ExampleController < ApplicationController

  get '/example' do
    title "Example Page"
    erb :example
  end

end
1

1 Answers

1
votes

It looks like, based off the tutorial, that you've used the route /example in both your config.ru file and in the routes for the ExampleController. This might mean your local url might end up as 'http://localhost:4567/example/example' . By the looks of things your example_controller.rb file should look like this, with '/' as the route:

example_controller.rb

class ExampleController < ApplicationController

  get '/' do
    title "Example Page"
    erb :example
  end

end

It also looks like you need to require 'sinatra/base' in your config.ru file.

config.ru

# Load Controllers and models
require 'sinatra/base'
Dir.glob('./{helpers,controllers}/*.rb').each { |file| require file }

map('/example') { run ExampleController }
map('/') { run ApplicationController }

Also your application_controller.rb appears to be missing your helpers and set views isn't included.

application_controller.rb

class ApplicationController < Sinatra::Base
  helpers ApplicationHelper

  # set folder for templates to ../views, but make the path absolute
  set :views, File.expand_path('../../views', __FILE__)

  # don't enable logging when running tests
  configure :production, :development do
    enable :logging
  end

  get '/' do
    title "Home.erb"
    erb :home
  end

  # will be used to display 404 error pages
  not_found do
    title 'Not Found!'
    erb :not_found
  end
end