Ruby on rails: convert text with html and rails code - html

Article in my app has field "body", which contains text with html. For example:
<div class ='skill_pic'>
<img src = "/assets/images/picture_1.png">
</div>
In view I call field "body" that:
<%= raw #article.body %>
In local machine all is ok, but when I deploy my app on production, images don't display right and browser console returns a 404 error.
image_tag in article body can solve my problem, I mean:
<div class ='skill_pic'>
<%= image_tag('images/picture_1.png') %>
</div>
But helper raw can't convert text with html and rails method
Now, I think in three directions:
find right path for images on production
find some helper, which can right convert text with html and rails code
find another solution

I suppose you want to do some kind of mini-CMS. Be careful not letting users fill in data in your database. You get really bad security problems. If user can edit your html, you better use some kind of templating engine like Liquid or markdown.
That being said, you should just not use the asset pipeline for the referenced images. put the images in the public folder instead of the assets folder. The public folder will be available like a web root '/', so without the 'public' in the url.

You could store erb partials like this in your database:
<div class ='skill_pic'>
<%= image_tag('images/picture_1.png') %>
</div>
And render them with ERB in your views:
<%= raw ERB.new(#article.body).result(binding) %>

Related

image_tag not rendered from Code in Database

I have stored html code in a database field which is passed to the rails app.
<%= raw #exercise.explanation %>
The normal text is rendered correctly, it´s in the <p> .. </p>; but inside there the image codes are just displayed so output on the page is:
This is correct rendered text.
<%= image_tag ("exercises/picture.png"), style: 'height:auto; width:50%;' %>
Further text.
When I use <img src="exercises/picture.png"> or <img src="picture.png">inside the database entry, no picture is loaded either, just the broken image symbol of the browser.
Do you have the Assets Pipeline on? If you do, this could happen because of the generated hash that it put on your static assets to avoid cache problems. The thing is that the image_tag helper resolves this. It's a trade-off, but you could disable the Assets Pipeline and see if it works. To keep it on and do this, you will need a more elaborated solution.
You can try if it is that by disabling the assets digesting:
config.assets.digest = false
Alternatively you can have ERB interpret #exercise.explanation
<%= ERB.new(#exercise.explanation).result(binding) %>
This will take your #exercise.explanation and process it through ERB which appears to be what you are hoping for.
Caveat:
Be very careful what you allow to be stored in these "template" fields as things can go bad if you do not/can not sanitize this input. ERB#result is essentially a call to eval (which can be very dangerous)

Creating dynamic links for a HTML email with embedded ruby

I have a simple question about links in HTML emails
I am trying to add a simple dynamic link in an HTML email to my site like this
MySite
This fails, but this works
MySite
I am assuming its to do with the embedded ruby but strangely, the links work if I view them on my iphone.
Any thoughts on this?
Stupid me....
I needed to construct the link using the ActionMailer generating URLs instructions, see here http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views
My URL is
<%= link_to "View response", notification_micropost_response_url(#micropost, #response) %>
The default host must be first set in the dev and prod environment config files:
Example (development.rb)
config.action_mailer.default_url_options = { :host => 'localhost:3000' }

Rails: Loading Image from Model

I am new to rails so forgive me if this is a basic question and I'm missing something.
I have a model that stores the location, filename, and extension of an image file. I concat those together and write them into an tag in my model using the image_tag. The tag is being correctly rendered in HTML with the proper path; however the image is showing up blank. I've added the image to app/assets/images so everything should be in its proper place but I can't figure out why the image isn't rendering. Here's what I have:
Model
class FileInfo < ActiveRecord::Base
def fullPath
"#{location}#{fileName}#{extension}"
end
end
View
<% #letter.relatedMedia.each do |m| %>
<%= image_tag(m.file_info.fullPath) %>
<% end %>
when the image tag renders in HTML, I get this as the source:
"/images/letters/some-image-file.jpg"
In my application, the file is stored here:
"app/assets/images/letters/some-image-file.jpg"
Am I storing the file in the wrong place? Is the path not writing out correctly? I'm really not sure where I'm going wrong here.
Any help you can provide would be greatly appreciated!
Change the path stored in the database to letters/some-image-file.jpg. You don't need the full path because the Rails asset pipeline will find the image for you.
Read about the Rails Asset Pipeline to get a better understanding about how it works.

output a variable in rails containing html code Ruby on Rails

I have a little problem with an output in ruby and rails.
I am still a beginner at rails, so it can be that the solution is pretty easy and i just can't see it.
I am trying to parse a website and put out some of the sourcecode on my own website.
Problem: it always puts out the whole source code as a text and is not interpreting the html code. how can i change that?
code:
page = Nokogiri::HTML(open("https://www.google.ch/search?client=ubuntu&channel=fs&q=google&ie=utf-8&oe=utf-8&redir_esc=&ei=WMpyUfSWEuz07AaTioCwDw"))
source = page.css('div#_css0')
<%= page %>
result:
http://postimg.org/image/dsaib9lx3/
I want to it to look like:
http://postimg.org/image/z9qlhoef5/
Thanks for any suggestions!
You should use raw in erb. It is actually equivalent to calling html_safe on it, but, just like h, is declared on a helper, so it can only be used on controllers and views.
page = Nokogiri::HTML(open("https://www.google.ch"))
<%= raw page %>
or
<%= raw Nokogiri::HTML(open("http://www.google.com")) %>
In Rails views every string content gets escaped, unless you use html_safe (docs)

Use HTML inside a Rails translation file

I have some translations in my Rails application (config/locale/[en|de].yml) and I use it in my views with <%=t "teasers.welcome" %>. Example:
teasers:
welcome: "<strong>Welcome</strong> to the Website ..."
In Rails 2.3.8 this works just fine, with Rails 3, the HTML is escaped and translated to <... How can I prevent this form this translation and use HTML in my translation files like in Rails 2.3.8?
Other than using raw, there's an other undocumented (but official) way to do so.
All keys ending with _html are automatically rendered unescaped.
Rename the key from
teasers:
welcome: "<strong>Welcome</strong> to the Website ..."
to
teasers:
welcome_html: "<strong>Welcome</strong> to the Website ..."
I suppose it's because doing
<%= t("blah") %>
in rails 2.x, now is the equivalent of doing
<%=h t("blah") %>
when you're using rails 3.
From the release notes:
Switch to on-by-default XSS escaping
for rails.
To fix this, and once again from the release notes:
You no longer need to call h(string)
to escape HTML output, it is on by
default in all view templates. If you
want the unescaped string, call
raw(string).
So just replace
<%= t("blah") %>
by
<%= raw t("blah") %>