Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Resetting form fields with page refresh

I've got a Rails form that has an observe_field which performs an ajax request when a drop down list is changed. The ajax causes the form to be re-rendered (the form is in a partial) with an extra param that causes some text on the page to change. All that works fine, but when I refresh the page (I'm running firefox), the text is reset and the drop down list does not change its value. Thus, I end up with a select value that does not correspond to the dynamic text.

I have tried setting a default selected value of the drop down, but for some reason firefox won't change the value with a page refresh.

This is the code for the drop down in the view:

<%= select_tag :category, options_from_collection_for_select(@categories, :letter, :name, @letter) %>

@letter is set dynamically and controls the dynamic text on the page.

This is the action that is rendered on a page refresh:

def new

@part = Part.new
@letter = params[:letter] || "A"
@part.cpn = Part.find_next_cpn(@letter)
@categories = PartCategory.find(:all)
respond_to do |format|
  format.js
  format.html
end

end

I need a way to either retain the dynamic text information or reset the drop down menu.

like image 646
Audie Avatar asked Jul 19 '10 20:07

Audie


1 Answers

You need to set the 'autocomplete' option off - this tells the browser that you don't want the field to autocomplete.

An example is here: http://mspeight.blogspot.com/2007/06/disable-browsers-autocomplete-on-rails.html

You can also do the same thing, but just turn off autocomplete for specific fields.

In other words, the form is not getting refreshed, because the browser is trying to be helpful by putting in the last data the customer used. You need to tell the browser to stop trying to be helpful. You're already providing the right data.

like image 82
jasonpgignac Avatar answered Nov 15 '22 05:11

jasonpgignac