3

So I've inherited this legacy rails codebase and there's a bug i need to fix where a given form is retaining values (that have not yet been saved to the DB) even after Page Refresh.

Use Case: User fills data in form but does not press submit. User Clicks refresh button on browser. The Values in the form are retained instead of refreshing the whole page.

Any ideas where I can start looking? In my brief, limited Rails experience, this is not the sort of thing that happens by default, so it must be coded somewhere?

Rex M
  • 142,167
  • 33
  • 283
  • 313
udit
  • 2,745
  • 3
  • 33
  • 44

4 Answers4

3

Sometimes browsers will do this. (I'm not talking about auto-fill drop-downs, which browsers also do... just talking about forms that are partially filled out, never submitted, and are then refreshed).

Make sure the values in the fields are actually coming from your Rails app by checking the HTML source window.

Values provided by the browser due to previous entry won't be in the VALUE attribute, but server-provided ones will be.

Some options for avoiding this might be:

  • Send the user to the form using a POST. Thus, any refresh results in a "do you want to resubmit" etc. I don't think the browser will pre-fill any fields in this situation (same as when a user posts and then hits the back button).
  • Use the onload() event to wipe out the form field values.

There's probably a simpler solution... Ctrl-F5 sometimes does the trick, but I think I've seen even that not work before.

richardtallent
  • 34,724
  • 14
  • 83
  • 123
1

Tell user to press CTRL+F5

-- edit if you can't tell the user to press CTRL+F5 (which always in my experience works unless the browser is buggy, or unless something else is going wrong), just trip form.reset() in the onload event. This will clear the fields in the form to the default values. If you have default values in the fields though, (like <input type="text" value="enter name here" />) then when you trip .reset(), it will reset to the default value.

Community
  • 1
  • 1
bobobobo
  • 64,917
  • 62
  • 258
  • 363
1

I just ran into this problem as well, and even <body onload="myform.reset()"> didn't work. This was particularly a problem because, for better or for worse, I'm using hidden inputs as counters (to keep track of a dynamic number of file upload fields, for example). These obviously need to be reset on reload to keep non-applicable warning messages from appearing. I "fixed" it for my in-house purposes by abandoning the reset button in favor of a link. Instead of <input type="reset"/> I'm using <a href=".">Reset</a> (the filename is index.php). Far from elegant, but it works for my purposes.

jared
  • 141
  • 10
0

I ran into this issue with a Rails 7 app. When navigating to a view with a form everything is blank, when I hit refresh all the inputs get filled by the browser (not by Rails). To keep the inputs blank I used the autocomplete attribute, which is supported by all modern browsers:

<!-- for the whole form -->
<form method="post" action="/form" autocomplete="off">

<!-- for a specific input element -->
<input type="text" id="foo" name="foo" autocomplete="off">
wintersolutions
  • 5,173
  • 5
  • 30
  • 51