Survey Form

We add a div and apply the Bootstrap text-center class for our form.

The code for the form is compact but complex.

We’ve mixed in a variety of divs for the form layout, using the
Bootstrap grid classes. It’s a lot of clutter but it stacks three form
elements at the center of the page.

We see several form elements:

    simple_form_for is the view helper for the form

The simple_form_for view helper instantiates a form object which we
assign to a variable named f. SimpleForm offers many standard form
elements, such as text fields and submit buttons. Each element is
available as a method call on the form object.

The view helper simple_form_for requires parameters and a block.

We initialize the form with the @visitor instance variable. SimpleForm
uses this parameter to name the form, set a destination for the form
data (the VisitorsController#create action), and initialize each field
in the form using attributes from the Visitor model. Setting the values
for the form fields from the attributes in the model is called “binding
the form to the object” and you can read about it in the RailsGuides:
Form Helpers article.

The simple_form_for view helper accommodates a Ruby block. The block
begins with do and closes with end.

Inside the block, the form object methods generate HTML for:

    error notifications
    a select box
    a field for “comment”
    a submit button

Each of the form methods takes various parameters.

The first input field creates a select box from parameters:

    :favorite – the field name matching an attribute in the Visitor
model
    :collection => Visitor::IMAGE_LABELS – choices from an array in the
Visitor model
    label: false – don’t display a label for the field
    as: :select – display the choices as a drop-down select menu
    prompt: 'Select a favorite...' – a prompt that appears above the
selections

If you study the SimpleForm documentation, you’ll see that as: :select
can be replaced with as: :radio_buttons or as: :check_boxes. You can use
radio buttons if you prefer.

The second input field creates a comment field from parameters:

    :comment – the field name matching an attribute in the Visitor model
    label: false – don’t display a label for the field
    :placeholder – a user prompt

The final method creates a submit button, setting the text to display in
the button and applying the Bootstrap classes btn btn-primary.

The structure of the form is clearly visible in the code. The form
begins with a simple_form_for helper and closes with the end keyword.
Each line of code produces an element in the form such as a field or a
button.