Need to reset and retain value in text_field


#1

Hi,
I have a form with a text_field in one of my views in RAILS. I need to:

  1. put a ‘reset’ button so that I can clear (set to blank) the
    text_field
    when the user clicks on the button

and

  1. retain value in text_field: I have a ‘search’ button that performs
    search
    (not Ajax livesearch… it’s a simple search and I wrote the code in my
    controller). Now, when the user puts some text (query) in the text_field
    and
    clicks on ‘search’, the results are shown ok, but I need to retain the
    text
    in the text-field that the user put in. Also I need to set the focus to
    this
    text_field after the results are shown.

The relevant code in the view is:
<%= start_form_tag :action => ‘search’%>
<%= text_field :server, :server_name, :size => 15 %>
<%= submit_tag ‘Search’ %>
<%= end_form_tag %>
<%= set_focus_to_id ‘server_server_name’ %>

Any help would be greatly appreciated. Thanks,
Arif

View this message in context:
http://www.nabble.com/Need-to-reset-and-retain-value-in-text_field-tf4131726.html#a11750514
Sent from the RubyOnRails Users mailing list archive at Nabble.com.


#2

On 7/23/07, arifin removed_email_address@domain.invalid wrote:

Hi,
I have a form with a text_field in one of my views in RAILS. I need to:

  1. put a ‘reset’ button so that I can clear (set to blank) the text_field
    when the user clicks on the button

<% form_tag(site_search_path, {:name => ‘search_form’}) do -%>
<%= text_field_tag(‘search_string’, @search_string) %>
<%= submit_tag(‘Search’) %>
<%= submit_tag(“Reset Form”, :onclick => “document.search_form.reset();”
) %>
<% end %>

the above will display a “Reset Form” button which will reset all the
field values in your form.

  1. retain value in text_field: I have a ‘search’ button that performs search
    (not Ajax livesearch… it’s a simple search and I wrote the code in my
    controller). Now, when the user puts some text (query) in the text_field and
    clicks on ‘search’, the results are shown ok, but I need to retain the text
    in the text-field that the user put in. Also I need to set the focus to this
    text_field after the results are shown.

you’ll have to store the search params in an ivar in your controller,
and then repopulate them into the search form using text_field_tag
like I did above with “text_field_tag(‘search_string’,
@search_string)” which will fill in the text field with whatever value
is stored in @search_string.

The relevant code in the view is:
<%= start_form_tag :action => ‘search’%>
<%= text_field :server, :server_name, :size => 15 %>
<%= submit_tag ‘Search’ %>
<%= end_form_tag %>
<%= set_focus_to_id ‘server_server_name’ %>

don’t use start_form_tag or end_form_tag, they’ve been deprecated.

Adam