RoR - Complicated default value for a text_area

49 views
Skip to first unread message

Quake Live

unread,
Aug 19, 2015, 8:21:01 AM8/19/15
to rubyonra...@googlegroups.com
Hi guys,

I got a small problem. I want to give a text_area a default value.
The default value is a range of numbers, seperated with a comma.

So the first thing which is given is a String that looks like followed:
some_string = "100,101,102,103,104,105"

Now there is a text_area given. It content should be the String
"some_string". The ouput should look like this here:

100,
101,
102,
103,
104,
105

So after each "comma" new line.

My idea was to convert the String to an Array and print each element of
the Array. But it did not work. Here is what it is looking like:

<%= f.text_area :number, :value => s.some_string.nil? "" :
s.some_string.split(',').each {|number| puts "#{number}," } %>


It will print this here:

["100","101","102","103","104","105"] which is not the result that i was
looking for. (mentioned above)


Any ideas?

--
Posted via http://www.ruby-forum.com/.

Hassan Schroeder

unread,
Aug 19, 2015, 8:31:48 AM8/19/15
to rubyonrails-talk
On Wed, Aug 19, 2015 at 5:19 AM, Quake Live <li...@ruby-forum.com> wrote:

> My idea was to convert the String to an Array and print each element of
> the Array. But it did not work. Here is what it is looking like:
>
> <%= f.text_area :number, :value => s.some_string.nil? "" :
> s.some_string.split(',').each {|number| puts "#{number}," } %>

Your first problem is using `puts` in a view; `puts` writes to stdout -
you want the values rendered in the web page.

Second problem is that a newline is just a whitespace character in
HTML; you probably want something like

s.some_string.split(',').join(",<br />").html_safe

HTH,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Elizabeth McGurty

unread,
Aug 22, 2015, 10:26:34 AM8/22/15
to Ruby on Rails: Talk
Just curious... What are your plans for this unusual use of a text_area?  Planning on a multiple select, perhaps? Will the display be read-only?  Liz

Walter Lee Davis

unread,
Aug 22, 2015, 11:26:07 AM8/22/15
to rubyonra...@googlegroups.com
As Hassan said, puts is not going to get it for you here. But a newline will work as a line-break inside the content of a textarea. If you store the string containing the newlines, and you show it inside a textarea, it will preserve the newlines. You won't need to do anything else to get it to work. Set the value as containing the newlines in the controller, and just display that default value in the view inside your textarea. If you then want to show these values in an HTML context with the newline characters replaced with break tags, then the easiest way to do that is to simply run it through simple_format in your view.

So in your controller, in the edit method, you might set this up:

@foo.number = @something.some_string || (100..105).to_a.join(",\n")

Then in your edit view, you could use

form_for @foo do |f|
...
f.text_area( :number )

...

If you have set up that value on the object of the form, then the text area will just populate with what you gave it.

On a show template, you just use:

simple_format @number

and you will get

101,<br/>
102,<br/>
103,<br/>
etc.

Walter


>
>
> Any ideas?
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/768aa6338c9e4344b1ea66db4020c508%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Hassan Schroeder

unread,
Aug 22, 2015, 3:06:38 PM8/22/15
to rubyonrails-talk
On Sat, Aug 22, 2015 at 8:25 AM, Walter Lee Davis <wa...@wdstudio.com> wrote:

> As Hassan said, puts is not going to get it for you here. But a newline will work as a line-break inside the content of a textarea. If you store the string containing the newlines, and you show it inside a textarea, it will preserve the newlines.

Gah, yes, my bad. Walter is correct - newlines are rendered as is
within the textarea. (I was thinking about outputting the result.)

I should know because I've used this before, i.e. to allow a user to
input a newline-separated set of response strings for a poll creation
tool. And it works fine :-)

Quake Live

unread,
Sep 18, 2015, 8:05:50 AM9/18/15
to rubyonra...@googlegroups.com
Hassan Schroeder wrote in post #1177877:
Sorry that I am answering that late. But this is exactly that what I was
looking for (one month ago :P).
Reply all
Reply to author
Forward
0 new messages