Text_area

Hi I’d like to know how to add default text text area using the function
text_area. I 've tried using value = “…” to do this but with no
luck.
Could some one please answer my simple question?

Thanks.
Dan.

Hi Daniel,

On 21.12.2005, at 11.29, Daniel Perrett wrote:

Hi I’d like to know how to add default text text area using the
function
text_area. I 've tried using value = “…” to do this but with no
luck.

If you look at the api docs for text_area [1], you can see that the
function (like most Rails view helpers) has an optional parameter
called “options”. Options is a hash that can contain values passed to
the resulting html tag as attributes. Since it’s a hash, you need to
specify the options like “value” => “…”.

So the whole function call would look something like

<%= text_area(“post”, “body”, “value” => “…”) %>

//jarkko

[1] Peak Obsession
FormHelper.html#M000345

Jarkko L. wrote:
I’ve just tried this, and I’m sure I have done before using this piece
of code.
<%= text_area( “user”, “tasks”, “value” => “This is a test”, :cols=>30,
:rows=>10) %>

It does not display “This is a test” in the text area!?!
The text area just comes up blank as before.
am I doing something wrong?

Cheers.
Dan.

Daniel Perrett wrote:

Hi I’d like to know how to add default text text area using the function
text_area. I 've tried using value = “…” to do this but with no
luck.

The text area is populated with the value of the associated variable
attribute.

For a page containing a new and empty form, you create (and pass to the
view) an empty object (see the “new” action generated by scaffolding).
If this object already contains the default value, the text area will
contain it too.

Jarkko L. wrote:

Oops, sorry. There is no attribute called value for the textarea html
element [1]. The actual value of the element is put between the tags.
However, you can prepopulate it by setting the @user.tasks to be the
default text in case of a new entry. The current value will be
automatically used as the default text by text_area.

//jarkko

[1] HTML textarea tag

Could you clarify how to do this with a example please?

On 21.12.2005, at 12.48, Daniel Perrett wrote:

Could you clarify how to do this with a example please?

In the controller you have something like
@user = User.new

After that, put this:
@user.tasks = “…”

Then the text_area helper will automatically put the dots as the
prepopulated value. Note that you shouldn’t be (and probably weren’t)
doing this in an edit (as opposed to create) form where the very same
form helper will populate the area with the real, existing value of
the db field “tasks”.

//jarkko

On 21.12.2005, at 13.30, Daniel Perrett wrote:

I don’t think this would work as I have no class called User!

I’ve given up and gone back to the old tried and tested method

some text it does the job.

Ah, you got to have an object at hand to use text_area, text_field
etc. helpers. It doesn’t matter what class it is, as long as it
responds to the method you specify in the second parameter.

Take a look at text_area_tag [1] for cases where you’re not playing
with objects. There you can set the content directly:

text_area_tag(“foo”, content = “bar”)

//jarkko

[1] Peak Obsession
FormTagHelper.html#M000411

Jarkko L. wrote:

@user = User.new

After that, put this:
@user.tasks = “…”

Then the text_area helper will automatically put the dots as the
prepopulated value. Note that you shouldn’t be (and probably weren’t)
doing this in an edit (as opposed to create) form where the very same
form helper will populate the area with the real, existing value of
the db field “tasks”.

//jarkko
I don’t think this would work as I have no class called User!

I’ve given up and gone back to the old tried and tested method
<textarea …>some text it does the job.

On 21.12.2005, at 12.09, Daniel Perrett wrote:

Jarkko L. wrote:
I’ve just tried this, and I’m sure I have done before using this piece
of code.
<%= text_area( “user”, “tasks”, “value” => “This is a
test”, :cols=>30,
:rows=>10) %>

It does not display “This is a test” in the text area!?!
The text area just comes up blank as before.
am I doing something wrong?

Oops, sorry. There is no attribute called value for the textarea html
element [1]. The actual value of the element is put between the tags.
However, you can prepopulate it by setting the @user.tasks to be the
default text in case of a new entry. The current value will be
automatically used as the default text by text_area.

//jarkko

[1] HTML textarea tag