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.
9bbb71666b26f777c9f250f172a8bbb3?d=identicon&s=25 Daniel Perrett (danielperrett)
on 2005-12-21 10:29
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.
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 Jarkko Laine (jarkko)
on 2005-12-21 10:50
(Received via mailing list)
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] http://api.rubyonrails.com/classes/ActionView/Helpers/
FormHelper.html#M000345
9bbb71666b26f777c9f250f172a8bbb3?d=identicon&s=25 Daniel Perrett (danielperrett)
on 2005-12-21 11:09
Jarkko Laine 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.
B7e3b81c9170cea9e20591bfb6a17dfe?d=identicon&s=25 noliv (Guest)
on 2005-12-21 11:12
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.
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 Jarkko Laine (jarkko)
on 2005-12-21 11:15
(Received via mailing list)
On 21.12.2005, at 12.09, Daniel Perrett wrote:

> Jarkko Laine 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] http://www.w3schools.com/tags/tag_textarea.asp
9bbb71666b26f777c9f250f172a8bbb3?d=identicon&s=25 Daniel Perrett (danielperrett)
on 2005-12-21 11:48
Jarkko Laine 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] http://www.w3schools.com/tags/tag_textarea.asp

Could you clarify how to do this with a example please?
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 Jarkko Laine (jarkko)
on 2005-12-21 12:00
(Received via mailing list)
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
9bbb71666b26f777c9f250f172a8bbb3?d=identicon&s=25 Daniel Perrett (danielperrett)
on 2005-12-21 12:30
Jarkko Laine 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</textarea> it does the job.
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 Jarkko Laine (jarkko)
on 2005-12-21 12:42
(Received via mailing list)
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
> <textarea  .........>some text</textarea> 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] http://api.rubyonrails.com/classes/ActionView/Helpers/
FormTagHelper.html#M000411
This topic is locked and can not be replied to.