Rails 表单生成器 - 如何放置 html 属性

Rails Form Builder - How to place html attributes

我有一个 server_form 迭代,我把 class: 'form-control' 它工作正常,除了最后一次迭代 |spec_fields|

= form_for @server do |server_form|
  = server_form.label :hostname
  = server_form.text_field :hostname, class: 'form-control'

  = server_form.label :description
  = server_form.text_field :description, class: 'form-control'

  = server_form.fields_for :spec do |spec_fields|
    Count of CPUs  : #{spec_fields.text_field :cpucount}
    RAM (GB): #{spec_fields.text_field :ram_gb}
    HD (GB): #{spec_fields.text_field :hd_gb}
    HD used (GB): #{spec_fields.text_field :hdused_gb}

我的问题是:如何将 form-control class 也放入 |spec_fields|

docs 你必须在块之前传递它所以......

= server_form.fields_for :spec, class: 'form-control' do |spec_fields|
    Count of CPUs  : #{spec_fields.text_field :cpucount}
    RAM (GB): #{spec_fields.text_field :ram_gb}
    HD (GB): #{spec_fields.text_field :hd_gb}
    HD used (GB): #{spec_fields.text_field :hdused_gb}

应该可以

fields_for 块之外进行操作:

= form_for @server do |server_form|
  = server_form.label      :hostname
  = server_form.text_field :hostname,    class: 'form-control'
  = server_form.label      :description
  = server_form.text_field :description, class: 'form-control'

  = server_form.fields_for :spec do |spec_fields|
    Count of CPUs  : #{ spec_fields.text_field :cpucount,  class: 'form-control' }
    RAM (GB):        #{ spec_fields.text_field :ram_gb,    class: 'form-control' }
    HD (GB):         #{ spec_fields.text_field :hd_gb,     class: 'form-control' }
    HD used (GB):    #{ spec_fields.text_field :hdused_gb, class: 'form-control' }