the text_field helper expects the first parameter to be an instance
variable. The second parameter is the method that will be called on
the instance variable to populate the value of the text field. So if
you have the following:
controller.rb
def new
@user = User.new(params[:user])
end
view.rhtml
<%= text_field :user, :first_name %>
the field will contain the value of the user's first name (providing
that you have a "first_name" attribute in your users table in your
database). If the user doesn't have a first name (ie the record is
new), then the value will be blank.
If you want more control about how the text field's value is
populated, you can use the following:
text_field_tag 'name of text field', @value
you should read through Agile Web Development for Rails if you haven't
done so already.. This type of stuff is outlined in the book.
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' %>
The controller code for 'search' is:
def search
condition = ""
var = ""
if !params[:server][:server_name].blank?
column_query = params[:server][:server_name]
var = params[:server][:server_name]
condition_part = "lower(s.server_name) like \'%" +
column_query.downcase + "%\'"
condition = condition + condition_part
and_flag = 1
end
...
if !condition.blank?
sql = "
select s.server_name, s.server_type_code from server s
where " +
"#{condition}"
...
@server_pages, @servers = paginate_by_sql Server, sql, 30
render :partial => "search", :layout => "application"