简体   繁体   中英

Rails Form Builder - How to place html attributes

I have a server_form iteration where I put class: 'form-control' It works fine, except of the last iteration with |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}

My Question is: How can I put the form-control class also into |spec_fields|

From the docs you'd have to pass it in before the block so...

= 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}

should work

As you do it outside the fields_for block:

= 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' }  

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