如何将我在Ramaze中的代码库划分成不同的控制器类,其中最“类似红宝石”的方法是什么?
我在Ramaze有一个基本项目,我想把它分成多个文件。现在,我使用一个控制器类对所有的东西,并添加到它与开放的类。理想情况下,控制器的每个不同部分都在自己的类中,但我不知道如何在Ramaze中这样做。
我希望能够添加更多的功能和更多的独立控制器类,而不需要添加太多的样板代码。我现在要做的是:
init.rb
require './othercontroller.rb'
class MyController < Ramaze::Controller
map '/'
engine :Erubis
def index
@message = "hi"
end
end
Ramaze.start :port => 8000othercontroller.rb
class MyController < Ramaze::Controller
def hello
@message = "hello"
end
end任何关于如何分割这一逻辑的建议都将不胜感激。
发布于 2015-01-14 13:01:33
通常的方法是在init.rb中要求控制器,并在它中定义自己的文件中的每个类,并在init中要求它,如下所示:
控制器/init.b:
# Load all other controllers
require __DIR__('my_controller')
require __DIR__('my_other_controller')控制器/我的控制器.my:
class MyController < Ramaze::Controller
engine :Erubis
def index
@message = "hi"
end
# this will be fetched via /mycontroller/hello
def hello
@message = "hello from MyController"
end
end控制器/my_other_控制员.my:
class MyOtherController < Ramaze::Controller
engine :Erubis
def index
@message = "hi"
end
# this will be fetched via /myothercontroller/hello
def hello
@message = "hello from MyOtherController"
end
end您可以创建继承自的基类,这样就不必在每个类中重复engine :Erubis行(可能还有其他行)。
如果您想在'/‘URI上为MyController服务,可以将其映射到'/’:
class MyController < Ramaze::Controller
# map to '/' so we can call '/hello'
map '/'
...您可以查看博客示例中的示例:https://github.com/Ramaze/ramaze/tree/master/examples/app/blog
https://stackoverflow.com/questions/27935766
复制相似问题