<%= f.text_area :comment, placeholder: "Comment on your track or" + \n + "share your favorite lyrics" %>
How can I get the placebolder to line break like
Solution was just to add white space so the next line wraps:
placeholder: "Comment on your track or share your favorite lyrics" %>
Pretty ugly but least complicated
The newline character \n should be included between the double, however HTML does not allow for line feed, but Thomas Hunter suggested an hack which consists in using a bunch of white spaces, like so:
<%= f.text_area :comment, placeholder: "Comment on your track or share your favorite lyrics" %>
You can also opt to use the title attribute instead.
In Ruby, in general "\n" is the new line character.
e.g.:
puts "first line\nsecond line"
=>
first line
second line
However, in your case:
you seem to try to use the newline character in an .erb expression <%= ... %>
That does not work, because that will only format the newline in the raw HTML souce,
but in the formatted HTML you will not see the newline! :-)
To see the newline in the formatted HTML, you need to do something like this:
either put the two strings in separate DIVs or SPANs
or put a <br /> in the string instead of the "\n" -- <br \> is the HTML newline symbol
You're creating HTML code. HTML does not care about whitespace in the actual code. What you need is a break in the HTML itself. However, it seems per this other question Can you have multiline HTML5 placeholder text in a <textarea>? that HTML does not allow line breaks in the placeholder field.
Related
I have a text_area tag which allows the user to enter his Bio. When a user is tyoing and if he hits enter or return, a new line starts. But when he saves his input all the text is displayed in one paragraph. I want functionality similar to what stack overflow has.
For example - I hit enter now
This text appears on a new line*
How can I do this?
This is my code in Rails:
<%= form_for :profile do |profile| %>
<%= profile.text_area :bio %>
<%= f.submit "Save Bio" %></p>
<% end %>
You should use text editor for example ckeditor (to simplify web content creation), and in view try simpleformat or raw:
<%= simple_format("Here is some basic text...\n...with a line break.") %>
<%= raw("Here is some basic text...<br/>...with a line break.") %>
There are many ways to handle this. When displaying text previously inputed in text area you can:
replace newline characters with <br/> tags
use <pre> tag and display text inside that tag
split text by newline characters and then wrap each of the chunks into <p> tags
When using approach 1 or 3, make sure to pass text through raw helper, so that any tags within text are displayed. Be aware though, that user may pass arbitrary html inside the textarea, hence your code may be subject to xss attacks.
i replaced line feed characters with HTML line break as
#post.description.gsub(/\n/, "<br/>")
I am trying to show the output within tags. But I see as follows:
Used, like new book<br/>New book costs $150<br/>Awesome book!
Need help. Thanks.
Use simple_format for this as it will automatically convert new lines into breaks.
<%= simple_format(#post.description) %>
I have Person.description with the following stored in the database:
jnkl
fdsfdsf
fdsf
fsdfdsfs fds fd sf sdf ds
How do I display this with the line-breaks in the view? It is currently displaying all on one line and I don't understand why.
you should use the simple_format helper:
<%= simple_format #person.description %>
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format
Source
# File actionview/lib/action_view/helpers/text_helper.rb, line 301
def simple_format(text, html_options = {}, options = {})
wrapper_tag = options.fetch(:wrapper_tag, :p)
text = sanitize(text) if options.fetch(:sanitize, true)
paragraphs = split_paragraphs(text)
if paragraphs.empty?
content_tag(wrapper_tag, nil, html_options)
else
paragraphs.map! { |paragraph|
content_tag(wrapper_tag, raw(paragraph), html_options)
}.join("\n\n").html_safe
end
end
The reason why, is that in plain HTML, outside of containing tags such as 'xmp', line breaks aren't rendered as linebreaks, for the most part they are ignored. For them to show up, you need to replace them with 'br' tags, or something else that has a style or structure associated with it, like p tags, or even divs, depending on the content is.
This should do what you ask:
#person.description.gsub(/\n/, '<br />')
The built in Rails helper simple_format will also work, using p tags
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format
I also use
#person.description.gsub(/\n/, '<br/>').html_safe
to display them in the view
Instead of replacing \n to <br> tags, which would consume a lot of processing if its a huge text, use css white-space: pre to break lines on \n.
Source: An html tag other than a textarea where \n is correctly interpreted
simple_format did not suit my needs as I want to be able to show multiple line breaks. simple_format makes 2 or more line breaks a paragraph.
So, I used css white-space: break-spaces; instead. The only current drawback is the browser support is 89.5% now.
I'm a noob at this and can't figure out why the hyphen gets added to something like this:
Not even sure if my jargon in the title of this question is accurate.
adding the "-" will remove the line break for that line
It means simply:
Place any text (HTML) that follows <% -%> on the next line in the rendered template.
The hyphen is not necessary in the above code. Simply adding <% end %> is sufficient to execute the embedded ruby.
The use of hyphen is fully explained here and essentially effects rendered html. In your case, what the hyphen does is this:
1 Hyphen at the end of the tag, just the two spaces
2 before the tag on the line below will be left
3 <% -%>
4 Last line
The code will output with two spaces before " Last Line" just below your <% -%> tag
It means that it will add the \n (or maybe \r\n, I forget which) to the line.
It just effects way the HTML is formatted.
So if did:
>> helper.image_tag "image.jpg"
=> "<img alt=\"Image\" src=\"/images/image.jpg\" />"
it would output something like:
"<img alt=\"Image\" src=\"/images/image.jpg\" />\r\n"
Meaning that your html page would look like:
<image tag>
<whatever other tag>
instead of having them both on the same line.
In the controller, I have an variable #xml_string = "<tag> hello \n world </tag>". Now I want to show the content of #xml_string. In erb file I wrote <%= #xml_string %>, but this can only display hello world, the xml tag <tag> </tag> was missed and \n was ignored.
Aslo , <% render :text => #xml_string , :content_type = 'application/xml' %> would not show any thing at all.
what is the correct way to achieve this? Thanks.
HTML ignores new line characters and white spaces unless you wrap the content into a tag that is whitespace-aware.
<pre><%=h #xml_string %></pre>
Otherwise, replace the "\n" with a line break. In this case you need to manually escape the HTML string.
<%=h #xml_string.gsub("<", "<").gsub("\n", "<br>") %>
try:
<%=h #xml_string %>
You could use this:
<%=h #xml_string.dump[1..-2] %>
The dump method will simply return the string in a way that makes str == eval(str.dump). That means it includes the quotes, so you need the [1..-2] to slice those away.