smc*_*ill 4 ruby ruby-on-rails
我的应用程序是表单构建器:定义表单,然后以通常的CRUD方式呈现激活的表单.激活表单的过程触发了FormManager创建主Form对象的子类(我使用STI ActiveRecord来创建type,使用的子类Object.const_set()).可以停用活动表单,这涉及杀死该子类定义(使用Object.const_send(:remove...))
我的应用程序只需要一个 FormManager对象.最好的方法是什么?我目前正在使用一个类变量ApplicationController来实现这个...它的工作原理,但似乎有点笨重:
require 'lib/form_manager.rb'
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
attr_reader :registry
protect_from_forgery
@@registry = FormManager.new
end
Run Code Online (Sandbox Code Playgroud)
我正在运行ruby 1.8.7,在开发模式下运行2.3.11 - 我看到这只是因为我处于开发模式?
MBO*_*MBO 10
不,它只是这样工作.Rails具有请求 - 响应模型,并且对于每个请求,它创建一些控制器的新实例(可能从您的ApplicationController继承),设置一些请求参数然后触发您的操作方法.如果要在请求之间共享状态,则需要将其置于控制器之外,例如,当应用程序由服务器启动时,将初始化为常量(它只是Ruby).
如果您需要单个注册表实例,只需将其放在"config/initializers/registry.rb"中:
require 'lib/form_manager.rb'
REGISTRY = FormManager.new
Template.all(:conditions => { :is_active => false }).each do |t|
REGISTRY.loadForm(t.id)
end
Run Code Online (Sandbox Code Playgroud)
然后在ApplicationController中:
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery
def registry
REGISTRY
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1084 次 |
| 最近记录: |