RoR - Complicated default value for a text_area

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?

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,

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

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)

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

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 :slight_smile:

Hassan Schroeder wrote in post #1177877: