简体   繁体   English

将重复的块传递给表单

[英]Passing a repeated block to a form

I have a form with multiple "li" elements that contain form fields, where the only thing that varies is one word in multiple placee. 我有一个包含多个包含表单字段的“ li”元素的表单,唯一不同的是多个放置位置中的一个单词。 Example: 例:

li
  = f.label :answer_tax, 'Tax'
  = f.text_field :answer_tax,
  'ng-model' => 'Form.tax',
  'ng-pattern' => '{{pattern}}',
  'ng-focus' => 'initFields(Form.tax)',
  'ng-blur' => 'updateScore(Form.tax, "tax")'

li
  = f.label :answer_chase, 'Chase'
  = f.text_field :answer_chase,
  'ng-model' => 'Form.chase',
  'ng-pattern' => '{{pattern}}',
  'ng-focus' => 'initFields(Form.chase)',
  'ng-blur' => 'updateScore(Form.chase, "chase")'

I would like to create a partial that takes each word from an array and generates the form fields. 我想创建一个局部变量,该局部变量从数组中提取每个单词并生成表单字段。 Where is the best place to do this (helper?) and how would I go about doing it? 最好的地方在哪里(助手?),我将如何去做? Thank you for your time. 感谢您的时间。

You could just do it in the view, you don't even need to use a partial if you feel this is simpler (i've assumed you're using HAML? If I'm right, you should have % on the li tag?): 您可以在视图中执行此操作,如果您觉得这样更简单,则甚至不需要使用局部变量(我假设您正在使用HAML?如果我是对的,则应该在li标记上包含% ?):

- %w{ tax chase }.each do |field_name|
  %li
    = f.label "answer_#{field_name}".to_sym, field_name.capitalize
    = f.text_field "answer_#{field_name}".to_sym,
    'ng-model' => "Form.#{field_name}",
    'ng-pattern' => '{{pattern}}',
    'ng-focus' => "initFields(Form.#{field_name})',
    'ng-blur' => "updateScore(Form.#{field_name}, \"#{field_name}\")"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM