Scorched icon indicating copy to clipboard operation
Scorched copied to clipboard

`<class:App>': uninitialized constant App::ControllerA (NameError)

Open tablecell opened this issue 2 years ago • 2 comments

require 'scorched'

class App < Scorched::Controller
  get '/' do
   'root'
  end

  self << {pattern: '/a', priority: 10, target: ControllerA}
end


class ControllerA < Scorched::Controller
  get '/' do
    "sub a at App"
  end

  self << {pattern: '/b', priority: 10, target: ControllerB}


end


class ControllerB < Scorched::Controller
  get '/' do
    "sub b  at a"
  end

end

Is Scorched must be strict order by [ControllerB ,ControllerA, App] ?

tablecell avatar Sep 21 '21 02:09 tablecell

Remember you're writing Ruby code here. You can't refer to a constant (e.g. class name) before it's defined. If your example, you're referencing ControllerA and ControllerB before they've been defined.

If defining your controllers as you are, you're best to require them all in advanced before mapping them in your main contoller. But like a lot of these things, there's always more than one way to do.

Wardrop avatar Sep 21 '21 05:09 Wardrop

@Wardrop Is there other way to require controllers like "best practice "?

tablecell avatar Sep 21 '21 07:09 tablecell