Rails: reset form fields with page refresh

I have a Rails form that has an observable field that executes an ajax request when the dropdown list changes. Ajax forces the form to re-display (the form is partial) with an additional parameter that causes the text on the page to change. All this works fine, but when I refresh the page (I start firefox), the text is reset, and the drop-down does not change its value. This way I get a selection value that does not match the dynamic text.

I tried to set the default value for the dropdown, but for some reason firefox will not change the value when the page is refreshed.

This is the code for the drop down list:

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

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

This is the action that appears when the page is refreshed:

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 code>

I need a way to save dynamic text information or reset a drop down menu.

+4
source share
1 answer

You need to set the parameter "autocomplete" - this tells the browser that you do not want the field to be autocomplete.

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

You can also do the same, but just turn off autocompletion for certain fields.

In other words, the form is not updated because the browser is trying to help by adding the latest data that the client used. You need to tell the browser not to try to be useful. You are already providing the correct data.

+5
source

Source: https://habr.com/ru/post/1316215/


All Articles