使用Ruby和JQuery构建表单生成器

使用Ruby和JQuery构建表单生成器,jquery,ruby-on-rails,functional-programming,closures,Jquery,Ruby On Rails,Functional Programming,Closures,我正在寻找一些关于为RoR应用程序设计上述内容的建议,该应用程序基本上有两个部分: 1) 管理员表单生成器2)最终用户表单(由管理员在步骤1中创建) 我已经原型化了我想要的JQuery表单(最终结果),它使用了“渐进式披露”(类似于JQuery in Action book中Yehuda Katz的竹子示例),因此如果单击一个项目,另一个项目可能会打开(例如单击“注册复选框”并显示另一个注册子表单)。我还让rails应用程序实际构建了两个输入,如前所述。然而,我觉得我并没有真正利用Ruby的动态

我正在寻找一些关于为RoR应用程序设计上述内容的建议,该应用程序基本上有两个部分:

1) 管理员表单生成器2)最终用户表单(由管理员在步骤1中创建)

我已经原型化了我想要的JQuery表单(最终结果),它使用了“渐进式披露”(类似于JQuery in Action book中Yehuda Katz的竹子示例),因此如果单击一个项目,另一个项目可能会打开(例如单击“注册复选框”并显示另一个注册子表单)。我还让rails应用程序实际构建了两个输入,如前所述。然而,我觉得我并没有真正利用Ruby的动态功能。例如,下面是我的一个模块方法的一部分,它构建了一个特定的简单html输入:

module Levin 
  module EventReg 
    module Jquery

  def self.simple_registration_text(input_params)
    raise ArgumentError if input_params.nil?
    ip_id = input_params[:input_div_id] 
    simple_reg_input_html = '<p><label for="'
    simple_reg_input_html << "#{ip_id + '_label_id'}\">"
    ........<A BUNCH MORE CODE TO BUILD INPUT>........
    simple_reg_input_html << ' class="required"' unless input_params[:is_required].eql?("0")    
    simple_reg_input_html << '/></p>'
  end
模块莱文
模块EventReg
模块Jquery
定义自我简单注册文本(输入参数)
如果输入参数为0,则引发ArgumentError?
ip\u id=输入参数[:输入分区\u id]

simple_reg_input_html='听起来可能与你的想法相反,但我认为你正在努力解决这个问题,因为你想要的部分是重新发明轮子(创建一种元语言,你正在使用的框架已经提供了工具)

我的方法大致如下:

使用某种javascript模板引擎(我的选择:或)为您的部分(在您的视图中)创建HTML模板。还没有建设者

<script id="form-partial-registration" type="text/x-handlebars-template">
 <div class="registration_field field">
  <label for="{{field_id}}" id="label_for_{{field_id}}" class="label registration"></label>
  <input id="{{field_id}}" />
 </div>
</script> 
然后,您就有了表单所需的每一段HTML代码的集合,这些代码显然是用HTML而不是javascript编写的。然后使用它们:

function build_some_form(has_registration, has_expiration, event_handlers) {
  var form_content = [];
  var form_prefix = '...some_prefix...'

  // something optional
  if (has_registration) { 
    form_content.push( render_template('registration', {field_id:form_prefix + '_reg'}) )
  }

  // another optional 
  if (has_expiration) { 
    form_content.push( render_template('expiration', {field_id:form_prefix + '_exp'}) )
  }

  // put it into the form
  $('#some_form').append(form_content.join(''));


  // hook up handlers, in this example for onclick only
  for(var k in event_handlers) {
    $('#some_form #' + form_prefix + '_' + k ).click(event_handlers[k]);
  }
}

build_some_form(true, false, {reg: function() { alert('registration clicked') })
我的观点是:试着专注于应用程序的“业务逻辑”,其余部分则力求简单(套用Joel Spolsky的话:深切关注您的核心竞争力,并尝试外包/伪造其余部分)


当然,这只是一个非常粗略的草案,我将采用这种方法并从这里进行改进(这里有一些重复,可以重构)。

@Rob,我正在从事一个类似的项目,您提供的链接非常有用。干杯您的示例代码非常“非Ruby”,但我感觉您知道这一点。我强烈推荐欧比的书《铁轨3路》。我最好的建议是要更加“鲁比主义”,那就是向后工作。不使用自定义代码构建第一个表单。然后构建你的第二个表单,也许还有第三个表单。一旦你看到你的复制将在哪里,然后重构。开始重构时,首先编写API。在你想“这件事的勇气如何发挥作用”之前,先想一想“我希望它如何发挥作用”。即使你没有先写测试(!!!),拥有这种心态还是很好的。您的代码将更清晰。+1用于链接演示文稿。非常好的阅读。你可以把一个字符串放在多行上。这将使您的代码更易于阅读。
function build_some_form(has_registration, has_expiration, event_handlers) {
  var form_content = [];
  var form_prefix = '...some_prefix...'

  // something optional
  if (has_registration) { 
    form_content.push( render_template('registration', {field_id:form_prefix + '_reg'}) )
  }

  // another optional 
  if (has_expiration) { 
    form_content.push( render_template('expiration', {field_id:form_prefix + '_exp'}) )
  }

  // put it into the form
  $('#some_form').append(form_content.join(''));


  // hook up handlers, in this example for onclick only
  for(var k in event_handlers) {
    $('#some_form #' + form_prefix + '_' + k ).click(event_handlers[k]);
  }
}

build_some_form(true, false, {reg: function() { alert('registration clicked') })