I am trying to get Ruby to search a text for certain characters, such as # and $. I then want to capture the character and the text beside it until there is white space, so if I would put this as text:
Hey, I am a #string and #what are you?
It would return the index at where the #string and #what are. This line only returns if the text has a special character:
<div class='tweet-text'>
<% if feed.text.match(/#\w+/) %>
Hi
<% end %>
</div>
The point here is I want to color the "#" word to a different color. The code is inside ejs. How would I do this?
When you want to highlight or mark one or more phrases in a text then you might want to have a look at the highlight view helper:
For example
<%= highlight('Hey, I am a #string and #what are you?', /#\w+/) %>
will return
Hey, I am a <mark>#string</mark> and <mark>#what</mark> are you?
Related
(edit: I've temporarily fixed this by simply placing it in a table, but I'd like to do cool things with each line styling wise and I feel like I'll be restricted with a table)
I'd like to display an array of strings in a view page with each string on its own new line. I have a Story model with columns for title and author_id, and a StoryLine model with columns for story_line, line_index, and story_id (my question does not concern this data structure - I'm confident that this is the right move for my use case). The story text consists of many lines without periods that are split on new lines, so when the text gets inputted, I parse them into the database in the create action as follows:
story_lines = params[:story_text].split("\n")
story_lines.each do |line|
#story_line = #story.story_lines.build
index = story_lines.index(line)
#story_line.line_index = index
#story_line.story_line = line
#story_line.save!
end
When a story gets opened by a user, all the story_lines get displayed in order of their index through the following code in the show action of the controller: #story_lines = #story.story_lines.order(:line_index).pluck(:story_line).join("\r\n")
They then get displayed in the view as follows: <%= #story_lines %>
However, for as many different methods as I've tried of changing this around in my controller and view, it's not displaying them on new lines. It's joining them successfully and in the correct order, but each sentence bumps up right up against the next one, with literally zero spaces inbetween the last word of one sentence and the first word of the next sentence.
Any ideas on how I should do this? I'm almost positive that I could do it by putting each newline in its own row of a table, but this seems like a cheap workaround for something that should be able to be handled with a <br />
What you need is more/better html :)
The problem is not rails - but the fact that this is what happens in a browser when you pump out a whole bunch of text.
If you want newlines to appear in html - then you need to use something that adds breaking-space.
You could use <br> for that (which is literally what br stands for) or you could use paragraphs eg:
<p> <!-- text should always be put inside a block-level object eg a paragraph -->
<% #story_lines.each do |line| %>
<%= line %><br> <!-- br example -->
<% end %>
</p>
<% #story_lines.each do |line| %>
<p><%= line %></p> <!-- p example -->
<% end %>
Obviously only choose one or the other or it'll be duplicated. :)
I have got a problem with Ruby on Rails template using HAML.
I have no idea how to apply a specific class inside a tag, for example in:
%p My name is John Doe
I want to add a .highlight class to "name" word. How to do this? I only know this way:
%p
%span My
%span.highlight name
%span is John Doe
but of course it is not the best way to do this, I hope so. Any ideas?
No matter what, you have to put the highlighted text in its own element, so your code is pretty close. You don't need to wrap the rest of the text in elements, though:
%p
My
%span.highlight name
is John Doe
This will produce:
<p>
My
<span class="highlight">name</span>
is John Doe
</p>
Edit:
In the case of question marks, you can use Haml's succeed helper method:
%p
= succeed "?" do
What's your
%span.highlight name
This will render:
<p>
What's your
<span class='highlight'>name</span>?
</p>
There are also precede and surround methods, which you can read about here: http://haml.info/docs/yardoc/file.REFERENCE.html#helper-methods
If you'd rather use string interpolation, it's probably be easiest to define your own helper method.
def hl(str)
capture_haml {
haml_tag 'span.highlight', escape_once(str)
}.strip!.html_safe
end
Since this is kind of elaborate, let me break it down from the inside out. First, we need to escape the string with escape_once. This is only necessary if str comes from user input (and so could lead to an XSS attack), but it's good to have in any event. Then we use haml_tag to render the string inside a span.highlight. Then we have to capture this output as a string with capture_haml, since we're using it in interpolation (ordinarily haml_tag writes to the Haml buffer instead of returning a string). Then we use strip! because Haml will render a newline after this, and you said you don't want that. Finally, we use html_safe to tell Rails not to escape the output, since it would break our <span> and we already escaped the input so we know it's safe.
Now you can use the hl method in your interpolated Ruby:
%p
What's your #{hl "name"}?
This will render:
<p>
What's your <span class='highlight'>name</span>?
</p>
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.
<%= 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.
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.