How can I put a link inside of a text string in HAML? - ruby-on-rails

This should probably be easier than it is. I just want to put a link inside an HTML paragraph element.
%p{class: "answer"}="Please upload your data to this portal in text (tab-separated) format. Download our template #{raw(link_to 'here', '/templates/upload_template.xlsx')} for sample data and a description of each column."
Rails is encoding the tag information. I don't want tags to be encoded. I want them to be tags.

You can use more than one line inside any block, to solve your problem we will have something like this:
%p{class: "answer"}
Please upload your data to this portal in text (tab-separated) format. Download our template
= link_to 'here', '/templates/upload_template.xlsx'
for sample data and a description of each column."

You can use interpolation directly in Haml, and doing this seems to fix the issue in this case.
So instead of doing this:
%p= "Text with #{something_interpolated} in it."
you can just do
%p Text with #{something_interpolated} in it.
i.e. you don’t need the = or the quotes, since you are just adding a single string. You shouldn’t need to use raw either.
(Also, note you can do %p.answer to set the class attribute, which may be cleaner if the value isn’t being set dynamically.)
Why this is being escaped here is a different matter. I would have expected the two cases (%p= "#{foo}" and %p #{foo}) to behave the same way. However, after a bit of research, this behaviour seems to match how Rails behaves with Erb.

Related

Change Encoding in ModelState.ModelError

Here is my problem: I add a message to ModelError with addModelError(String.Empty,”My message”).
In my view I just call #Html.ValidationSummary().
The message is in German and the characters Ö, Ä, Ü are just shown as questionmark. How do I change that?
As I see it there are two options. One option is to write a custom validation summary helper which doesn't HTML encode the messages like described in the link that Kartikeya Khosla provided. Or, and that’s what I did, Just use the Unicode reference in the message string. The solution in Kartikeya is more elegant, but in my case it is a lot of code to change two characters. By the way here a link to look them up if anybody wants to do the same:
http://www.utf8-chartable.de/unicode-utf8-table.pl?utf8=oct&unicodeinhtml=dec&htmlent=1

Replace URL in Text Body with an Image Tag for that URL

As the title suggests I would like to find a way to replace URLs within a body text (for a blog) with image tags for those URLs. I suspect I will need to do some form of regex. Has anyone done something like this before?
EDIT:
To describe the use case a bit more, I have a blog-esque site I am building. I would like blog writers to be able to 'drop' urls into text posts (separated by newlines), and have rails intelligently parse the string and replace any urls with images (perhaps in a helper method).
The sanest approach is to use something like Markdown (or exactly like it) and ensure that your posts are marked up correctly. This seems to be the most up-to-date gem for Markdown, https://github.com/vmg/redcarpet.
Alternatively, if you want to do this by yourself, it would still be prudent to mark up a link in some way. For example, {image src=link_to_the_image_here}.
This will make finding images within the body of text easier.

How do you handle inline tags with angular-gettext?

for instance:
<div translate>
This is <strong>awesome</strong>
</div>
I would like to know if there's a better approach to prevent sending those inlinr tags to the .po file.
I try to avoid putting markup in my localized strings when I can, but sometimes it is unavoidable.
However in the case of em and strong, they may need to be left in. Check out the answers to this question and you'll see that in some cases the translator may need to change or remove those tags altogether.
Another case that comes up often is inline links in text. You might consider putting the actual url for the link in a variable and adding a comment not to change the text inside braces.

how to force long URL/text to wrap in table cell when generating PDF in rails?

I am using princely(https://github.com/mbleigh/princely) to generate PDF in rails. I have long url in one table cell. It will extend the margin when I generate the PDF. In html,"word-break: break-all;" works well. But this rule "word-break: break-all;" doesn't work in PDF. Any body have any idea to wrap the long text when generating PDF?
Since princely is converting the ERB to PDF i believe we can use the truncate helper function of rails to make the link shorter.
= link_to truncate("The anchor you want to place", :length => 5), 'http://yoururl'
I was confronted with a similar issues when printing. Aside from the technical problem, you need to ask yourself some design questions: If you were generating a web page, a user would click the link. But if you're generating a PDF, I assume the goal is to print it. In which case, someone needs to type in this long URL.
How likely are the users going to be to type in a long url? If it only makes sense in the context of a live webpage, maybe you want to eliminate that content from the PDF.
If they do need to visit the destination, but they don't need to see the exact URL, then it may make more sense to shorten the URL.
If you want to shorten the URL, you can implement a URL redirection service on your Rails app. I like the following bit of code to generate the short URL code because they are all keyboard-friendly (they don't contain confusing characters, and don't require lots of shifting or switching of keyboards):
def generate_short_murl
a = [('a'..'k'),('m'..'z')].map{|i| i.to_a}.flatten
n = [('2'..'9')].map{|i| i.to_a}.flatten
(0...4).map{ a[rand(a.length)] }.join + (0...3).map{ n[rand(n.length)] }.join
end

Clean up & style characters from text

I am getting text from a feed that has alot of characters like:
Insignia&#153; 2.0 Stereo Computer Speaker System (2-Piece) - Black
4th-Generation Apple® iPod® touch
Is there an easy way to get rid of these, or do I have to anticipate which characters I want to delete and use the delete method to remove them? Also, when I try to remove
&
with
str.delete("&")
It leaves behind "amp;" Is there a better way to delete this type of character? Do I need to re-encode the text?
String#delete is certainly not what you want, as it works on characters, not the string as a whole.
Try
str.gsub /&/, ""
You may also want to try replacing the & with a literal ampersand, such as:
str.gsub /&/, "&"
If this is closer to what you really want, you may get the best results unescaping the HTML string. If so try this:
CGI::unescapeHTML(str)
Details of the unescapeHTML method are here.
If you are getting data from a 'feed', aka RSS XML, then you should be using an XML parser like Nokogiri to process the XML. This will automatically unescape HTML entities and allow you to get the proper string representation directly.
For removing try to use gsub method, something like this:
text = "foo&bar"
text.gsub /\b&\b/, "" #=> foobar

Resources