# Text\_area

**URL:** <https://www.ruby-forum.com/t/text-area/93608>\
**Category:** Rails\
**Created:** [April 5, 2006, 3:17am UTC](https://www.ruby-forum.com/t/text-area/93608 "2006-04-05T03:17:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [April 5, 2006, 3:17am UTC](https://www.ruby-forum.com/t/text-area/93608/1 "2006-04-05T03:17:59Z")

</div>

Sorry guys - I don’t want to be a help vampire, but I’ve been through a  
ton of tutorials, and they’re all written by guys who obviously have a  
solid backing in programming already because they seem to plow through  
without really explaining how things work in a way I (i.e. idiots) can  
understand.

Anyway, thanks to everybody who helped me yesterday. You helped me send  
a value to a view. Now I’m trying to send a value from a text field back  
to the controller to check and see if it’s the correct answer. But I  
don’t quite understand how Rails wants me to do this.

Here, for example:  
\<%= start\_form\_tag :action =\> ‘answer’ %\>  
\<%= text\_field ‘questions’, ‘answer’ %\>  
\<%= submit\_tag ‘check answer’ %\>  
\<%= end\_form\_tag %\>

I went to the Rails API and found the explanation of text\_field: the  
first value is the object, and the second value is the method. But I  
already set up the method that I want to go to in the start\_form\_tag  
part. So for some reason I have to call it twice? But my biggest issue  
is, where do I put the value that the user will enter into the form  
field? And how do I retrieve that value once I’m in the ‘answer’ method  
in the controller? I’m used to PHP and HTML forms, where you specify a  
name=“something” and then you can get the variable called “something”  
from the \_POST array. But how does Rails do this? Obviously there’s no  
\_POST array, but what is the Rails equivalent?

Again, thanks and apologies in advance for being the newbie with all the  
questions.

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [April 5, 2006, 3:36am UTC](https://www.ruby-forum.com/t/text-area/93608/2 "2006-04-05T03:36:40Z")

</div>

Sean:

The “method” to which you’re referring is really an attribute of an  
object. That is, it’s a field on an object that is going to be in the  
parameters of the post. The :action symbol of the start\_form\_tag  
tells Rails that you want to call the answer action when the user  
submits the form. So, you’re not actually doing anything twice.

Regarding your biggest issue, if the object that you’re using already  
has a value, then the form field will display that automatically. To  
retrieve that value, you can access the params associative array in  
your answer method of your controller.

If you haven’t already used the scaffold command, give it a try to  
get some generic CRUD views and controller. Study how the objects are  
instantiated for display on the edit page, and how a different action  
is called (update) when the form is submitted. It really helped me  
understand what was the intention of the framework.

Hope it helps,

-Anthony

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [April 5, 2006, 3:42am UTC](https://www.ruby-forum.com/t/text-area/93608/3 "2006-04-05T03:42:42Z")

</div>

Sean,

A couple things to sort out here:

In the start\_form\_tag you have an ‘action’. An action is a method in a  
_controller_.

Your text\_field documation shows it defined like this:  
_text\_field_(object\_name, method, options = {})

In this case ‘object\_name’ is most likely the name of an active record  
model  
object. The method mentioned must be a method of the model object.

Now, I’ll try to answer your specific questions:

> But my biggest issue  
> is, where do I put the value that the user will enter into the form  
> field?

I don’t understand your question. I think you are asking how to store it  
in  
the database. If so, you need an active record model object. Do you have  
a  
copy of the rails book? (agile web development with rails).

> And how do I retrieve that value once I’m in the ‘answer’ method  
> in the controller?

In the controller you can refer to whatever was typed into the text area  
by  
params[:questions][:answer], as in

```
obj.answer = params[:questions][:answer]

```

Or more commonly

```
obj.update_attributes(params[:questions])

```

It occurs to me that you may not care to store this in a database –  
that  
you just want to check the answer. If that is the case then you are  
using  
the wrong method. Instead of text\_field, you should use text\_field\_tag.

text\_field is specifically about connecting active record models to  
forms.  
text\_field\_tag is simply a tag and you can do whatever you want with the  
result – in other words you don’t need a model object.

So if you simply want an answer from a form, use text\_field\_tag. It will  
be  
something like

text\_field\_tag ‘answer’

In your controller, in this case, you will check params[:answer].Note  
that  
there is no longer a reference to :questions.

Good luck.

-Kelly

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [April 5, 2006, 5:42am UTC](https://www.ruby-forum.com/t/text-area/93608/4 "2006-04-05T05:42:24Z")

</div>

Thanks Anthony and Kelly -

I am, in fact, on chapter 9 of that Rails book as we speak. 🙂

But it’s turning out to be more difficult than I thought. The book’s  
basic explanations are very, very basic, bordering on the purely  
theoretical, and the implementation examples are too advanced. That bit  
about using text\_field for saving data in a database and text\_field\_tag,  
for example, I would never have found out if Kelly hadn’t been kind  
enough to clue me in (Thanks Kelly - you saved me a ton of wasted time).  
Is there a resouce out there somewhere that can help me figure out this  
middle ground?

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [April 9, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/text-area/93608/5 "2006-04-09T20:48:06Z")

</div>

Give up rails now if you can’t follow the book. It’s as easy as its  
gona get.

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [April 9, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/text-area/93608/6 "2006-04-09T20:42:05Z")

</div>

Is there a resouce out there somewhere that can help me figure out this  
middle ground?

I don’t have a good answer for you. I’ve spent a lot of time digging  
through  
the available docs at [http://api.rubyonrails.com/](http://api.rubyonrails.com/), the rails book, and  
the  
rails sources. One thing cool about this stuff is you most likely have  
all  
the sources right there on your machine. I’m constantly searching  
through  
them and adding print statements to try to understand what is happening.

-Kelly
