Abiodun
Abiodun

Reputation: 467

Sinatra App - Separating Concerns

Probably something really basic, but I want to be able to separate my Sinatra routes from controllers. I have this code in my routes.rb:

require 'sinatra/base'

class Server < Sinatra::Base
  get '/' do
   Action.index
  end
end

This is my controller/server.rb

class Action
  def sef.index
     @user = User.new("Abiodun Shuaib")
     haml: index
  end
end

It gives the error undefined method 'haml' in Action:Class.

How can I fix this?

Upvotes: 0

Views: 315

Answers (1)

Igor Pavlov
Igor Pavlov

Reputation: 720

You are trying to access method haml in class Action. It simply doesn't contain it. For example, you can do:

class Server
  def index
    @user = User.new("Abiodun Shuaib")
    haml :index
  end
end

By doing this, you will add to Server method index.

Or you can do in such way(it's called Mixin):

module ActionNew
  def index
    @user = User.new("Abiodun Shuaib")
    haml :index
  end
end

class Server < Sinatra::Base
  include ActionNew
  get '/' do
    index
  end
end 

Upvotes: 3

Related Questions