Forum: Ruby on Rails Need to reset and retain value in text_field

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Niaz A. (Guest)
on 2007-07-23 22:56
(Received via mailing list)
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

2. 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-val...
Sent from the RubyOnRails Users mailing list archive at Nabble.com.
Adam C. (Guest)
on 2007-07-24 00:14
(Received via mailing list)
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.

> 2. 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
This topic is locked and can not be replied to.