简体   繁体   中英

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. 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?):

- %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}\")"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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