Forum: Ruby on Rails text_area

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.
241b11f59c4bcfc2ce84a43f52b30001?d=identicon&s=25 Sean Cahoon (osakaboy)
on 2006-04-05 03:17
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.
8f3739e3b34bfea19fd381500d8a8fe6?d=identicon&s=25 Anthony Carlos (acarlos)
on 2006-04-05 03:36
(Received via mailing list)
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
6559f9b9607a89074ea5e238d31ffbc2?d=identicon&s=25 Kelly Felkins (Guest)
on 2006-04-05 03:42
(Received via mailing list)
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
241b11f59c4bcfc2ce84a43f52b30001?d=identicon&s=25 Sean Cahoon (osakaboy)
on 2006-04-05 05:42
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?
6559f9b9607a89074ea5e238d31ffbc2?d=identicon&s=25 Kelly Felkins (Guest)
on 2006-04-09 20:42
(Received via mailing list)
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/, 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
4a2e23da65a5f0b2f156797537a125e8?d=identicon&s=25 Gd (Guest)
on 2006-04-09 20:48
Give up rails now if you can't follow the book.  It's as easy as its
gona get.
This topic is locked and can not be replied to.