Search code examples
ruby-on-railsruby-on-rails-5user-experienceform-for

How do you ensure address form autofills correctly?


I have a form in Rails, part of which asks for an address:

  <%= f.fields_for :postal_address do |ff| %>
    <%= ff.text_field :apartment_number %>
    <%= ff.text_field :building_name %>
    <%= ff.text_field :building_number %>
    <%= ff.text_field :street %>
    <%= ff.text_field :town %>
    <%= ff.text_field :postcode %>
  <% end %>

However, when I click the little autofill thing in Safari (below) the form is filled in incorrectly.

autofill thing

This is obviously wrong:

enter image description here

Is there any way I can instruct the browser to autofill the fields with the expected data? Or is it solely up to the browser?


Solution

  • It should work better if you add autocomplete attribute to inputs. Browser tries to guess about field's purpose from its name, but not sure if it knows "Building number" and "Town"

    <%= ff.text_field :town, autocomplete: 'address-level2' %> # 'city' should work too
    

    You can check list of available values in the docs