# The value in text\_field helper

**URL:** <https://www.ruby-forum.com/t/the-value-in-text-field-helper/109979>\
**Category:** Rails\
**Created:** [July 20, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/the-value-in-text-field-helper/109979 "2007-07-20T21:39:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 20, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/the-value-in-text-field-helper/109979/1 "2007-07-20T21:39:27Z")

</div>

I have been working on this small, minute, part on my project and it all  
hinges on setting the value in a text\_field helper. Saying:

\<%= text\_field ‘user’, ‘company’, :value =\> @login %\>

does not do it. When I go and look at the raw html, there is no value  
there and “value” is an attribute of the input tag, im pretty sure. I  
really like RoR and how things work, but they fail miserably when it  
comes to documentation. Anybody know of any good reference sites besides  
[http://api.rubyonrails.org/](http://api.rubyonrails.org/)? Thanks,

~S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 20, 2007, 10:08pm UTC](https://www.ruby-forum.com/t/the-value-in-text-field-helper/109979/2 "2007-07-20T22:08:11Z")

</div>

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 D. for Rails if you haven’t  
done so already… This type of stuff is outlined in the book.

Adam

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 21, 2007, 12:13am UTC](https://www.ruby-forum.com/t/the-value-in-text-field-helper/109979/3 "2007-07-21T00:13:48Z")

</div>

Yeah, it works, to a point. My issue is that you start out at a  
“traveler\_form.rhtml.” On submit, the :action ‘create\_user’ is performed  
and when the log-in fails a redirect\_to reloads the traveler\_form. But  
before that, the controller for the traveler\_form, the ‘traveler\_form’  
method gets called, correct? At least this is the way that I understand  
the flow of data in the program. The information basically is getting  
lost in this loop. I’m just trying to figure out what I’m missing or not  
understanding? Thanks

~S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 21, 2007, 1:39pm UTC](https://www.ruby-forum.com/t/the-value-in-text-field-helper/109979/4 "2007-07-21T13:39:21Z")

</div>

simply don’t use a redirect, but

render :action =\> :traveler\_form

On 21 Jul., 00:13, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 23, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/the-value-in-text-field-helper/109979/5 "2007-07-23T21:42:19Z")

</div>

Hi,  
I have a similar problem:  
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’ %\>

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”

Your help would be greatly appreciated. Thanks.
