On Aug 19, 2015, at 8:19 AM, Quake Live <lists@ruby-forum.com> wrote:
> 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
>
>
> 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-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@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.
--
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-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/70843CB0-102A-47E8-8E04-39C772B7E9A6%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment