Turn autocomplete off with rails form_tag
Asked Answered
J

5

25

I'm trying to turn autocomplete off in one of my rails forms. Here's what I've tried so far:

# add it at the form level
= form_tag admin_sessions_path, autocomplete: "off" do

That didn't work.

# add it at the element level
.panel
  = label_tag :email
  = email_field_tag :email, params[:email], autocomplete: 'off'

Neither did that.

# add it to both
= form_tag admin_sessions_path, autocomplete: "off" do

  # add it at the element level
  .panel
    = label_tag :email
    = email_field_tag :email, params[:email], autocomplete: 'off'

When I visit my form, my email address and saved password are already filled-in. What am I doing wrong here? I love rails, but it really drives me mad sometimes.

Janae answered 16/7, 2014 at 13:9 Comment(0)
S
45

As of Rails 4+

Disable autocomplete for an entire form with form_tag:

= form_tag admin_sessions_path, html: { autocomplete: "off" } do

Disable autocomplete for an entire form with form_for:

= form_for @user, html: { autocomplete: "off" } do |f|

Disable autocomplete for a single form element:

= f.text_field :foo, autocomplete: 'off'

Hope this helps!

Stonechat answered 19/1, 2016 at 7:27 Comment(6)
Great timing - I posted this question (or similar) yesterday and only just found your answer today. Works perfectly - thanks!Yanyanaton
Glad it was helpful. :-)Stonechat
@MattSanders using html wasn't working for me and after reading the docs state Any other key creates standard HTML attributes for the tag, I just removed the html and simply had form_tag my_path { autocomplete: "off" }, which worked for meWitherite
@Witherite - thanks for the update. What version of rails are you using? The behavior may have changed in newer versions.Stonechat
5.1 docs referenceWitherite
I always forget to use the html: {} hash to set the option. Thanks!Netsuke
C
5

the autocomplete attribute should be assigned to the html key like so:

html: {autocomplete: 'off'}
Crass answered 16/7, 2014 at 13:18 Comment(0)
M
5

In latest version of chrome autocomplete: 'off' does not work. Instead use autocomplete: 'disabled'

Matroclinous answered 21/8, 2019 at 4:0 Comment(2)
NOTE: In Internet Explorer: "Any string other than off enables AutoComplete."Leilanileininger
Also, "As of Internet Explorer 11, the autocomplete property is no longer supported for input type=password fields."Leilanileininger
K
1

There several ways of turning off the autocomplete functionality:

On form level: (autoceomplete turned of for all inputs)

<% form_tag(:form_name, @form_name, autocomplete = "off") do |f|%>

Per input:

<%= text_field_tag('my input', nil, autocomplete = 'off') %>

Simple Form per input:

<% f.text_field :fieldname, input_html: {autocomplete: 'off'} %>
Karlsruhe answered 16/7, 2014 at 13:18 Comment(4)
Thanks but none of those options work with form_tag. The first and third suggestions work with form_for (and derivatives like simple_form), and the second one generates the following html: <input id="email" name="email" options="{:autocomplete=>&quot;off&quot;}" type="email">. The docs for form_tag show that there is not option called :options.Janae
Sorry... There is no option called :options, you're absolutly right. Just add your additional attributes like in my edited version. This way you're even able to add "foo='bar'. Same for the form_tag... See API Documentation: apidock.com/rails/ActionView/Helpers/FormTagHelper/…Karlsruhe
Ha, your new suggestions cause rails to throw an exception :) autocomplete = 'off' is valid html, but not ERB or HAML. Anyway, I think I must have done something funny, like save my password before turning autocomplete off. Perhaps I will have some luck if I delete my saved passwords and try again with autocomplete off from the start. Thanks again.Janae
Hehe... Okay. But as described in the documentation it supposed to work by adding additional options in a row with simple "equals'. I have no project using form_tag so I had to sort of guess. I appreciate the convenience of form_for or formtastic. Best of luck!Karlsruhe
L
0

Edit: Unfortunately, this method has drawbacks

  1. Fields are not filled back after validation errors and render :xxx
  • solution to this issue is to make alias_attribute :c_ountry, :country, but personally I don't like the hackyness of it
  1. Method does not work on English locale (at least for me), but works on other locales

Edit 2: Ultimately I solved it with a combination of three approaches which I described in more general question

Original answer:

It's the fault of HTML and mostly Chrome, not Rails.

As of December 2020 and Chrome 87 all of the above solutions didn't worked for me, neither do many variations from the web with autocomplete, readonly, onclick, type, etc. attributes' workarounds.

Solution to prevent Autofill

What worked is to not name the field with their full names. i.e. if we have country and city fields, then instead of this

= f.text_field :country
= f.text_field :city

do this

= f.text_field :c_ountry
= f.text_field :cit_y

Then manually reassign params in controller. i.e.

def user_params
  params[:user][:country] = params[:user].delete(:c_ountry)
  params[:user][:city] = params[:user].delete(:cit_y)

  params.require(:user).permit(:country, :city)
end

Gotcha: I noted that Chrome is triggering autofill also when it finds other words in type. For example, I observed that this triggers autofill

= f.text_field :d_istrict

while this don't trigger autofill

= f.text_field :d_istric_t
Lineal answered 18/12, 2020 at 23:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.