Escape ruby code inside HTML string - ruby-on-rails

I have an object in my controller
#data="<meta property=\"og:url\"
content=\"<%=SITE_URL%><%=request.request_uri%>\"
/>".html_safe
When I print this in my view file by <%=#data%> the html that it generates is
<meta property="og:url"
content="<%=SITE_URL%><%=request.request_uri%>"
/>
How do i print out/escape the ruby code withing the string?

You're looking for string interpolation, which means that in the string, it substitutes the value of the variable. Surround the variables with #{} like this:
http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation
#data="<meta property=\"og:url\"
content=\"#{SITE_URL} #{request.request_uri}\"
/>".html_safe
Now, when you do <%= #data %>, it should show up normally.

#data="<meta property=\"og:url\"
content=\"<%= #{SITE_URL}%><%= #{request.request_uri} %>\"
/>"
In your views use raw helper method
<%= raw(#data) %>

You can try h helper like this.....
<%=h(#data) %>
also raw can be use
<%=raw(#data) %>

Related

Rails tag syntax with multiple data attribute values quotes excluded

Trying to generate
<img data-interchange="[assets/img/interchange/small.jpg, small], [assets/img/interchange/medium.jpg, medium], [assets/img/interchange/large.jpg, large]">
is challenging.
<%= tag "img", data: { interchange: ["[#{photo.image_url(:base).to_s}, small]", "[#{photo.image_url(:big).to_s}, medium]", "[#{photo.image_url(:desktop).to_s}, large]"] } %>
will generate HTML, but the result is still inoperative
<img data-interchange="["[https://[...]base_20160529_115006.jpg, small]","[https://[...]big_20160529_115006.jpg, medium]","[https://[...]desktop_20160529_115006.jpg, large]"]" />
as " gets generated.
What is the proper syntax to get the proper HTML string
In rails you can use ActiveSupport's to_json to encode things into json.
<%= tag "img", data: { interchange: [[photo.image_url(:base), 'small'], [photo.image_url(:big), 'medium'], [photo.image_url(:desktop), 'large']].to_json } %>
Which produces
<img data-interchange="[[\"assets/images/interchange/base.jpg\",\"small\"],[\"assets/images/interchange/big.jpg\",\"medium\"],[\"assets/images/interchange/desktop.jpg\",\"large\"]]" />
Then on the front-end you can use JSON.parse once you retrieve the value via getAttribute or with jQuery data, if you need to use the data on the front-end.
var text = JSON.parse(element.getAttribute('data-interchange'));

Rails - use code(css, js, html) from server on client side

I had a application rails built with bootstrap and simple form.
Here I have to show the UI patterns how they actually look like. That means I have to show the
patterns like menu bar, accordian patterns examples in my application. For that I am storing the pattern code html,css,js in database.
Here my requirement is I have to show the actual code pattern view from the stored record(css,js,html) without any css/js conflicts.
How can eneter the html,css,js code dynamically in a partial or page to show that
in a fancybox in rails.
Thanks for the help in advance.
just use html_safe or raw to render your content as a normal string on views. For instance:
in your controller:
#x = YOUR_CODE_FROM_DB
in your view:
<%= #x.html_safe %>
# <%= raw #x %> is also ok in this case
NOTICE: you can use html_safe on models, but raw is declared on a helper, so you can just use it on controllers and views.
-- edit --
more example:
on controller:
#hello = 'alert("hi");'
#body = 'body{ background: red; }'
on view:
<script type="text/javascript" charset="utf-8">
<%= raw #hello %>
</script>
<style type="text/css" media="all">
<%= #body.html_safe %>
</style>

Ruby .to_sentence

In the .to_sentence method, How can I get the last_word_connector to output an HTML string?
For example:
locations.to_sentence last_word_connector: '<br />'
I want the <br /> to actually be interpreted. I have tried adding .html_safe on the end but to no avail.
You'll need to tell the view that the string is "safe" to insert (i.e. it should be inserted as-is - not escaped):
<%= locations.to_sentence(last_word_connector: '<br />').html_safe %>
The html_safe method returns a string that's marked as, well, html-safe. So it won't be escaped.
Use with caution. The reason Rails escapes string by default is that it makes html- and javascript-injection attempts harmless. So if you're not completely sure of the content of locations, then be careful.

very simple ruby combobox - text parse prolem

tried to create simple dropdown in ruby - like that :
<%= select_tag(:origin_id, '<option value="1">Lisbon</option>') %>
and I received an empy one.
this is the generated html
<select id="origin_id" name="origin_id"><option alue="1">Lisbon</option>/select>
Mark your HTML as safe to avoid Rails to escape it.
<%= select_tag(:origin_id, '<option value="1">Lisbon</option>'.html_safe) %>

Need Help on a custom HTML Helper

I'm trying to create a custom HTML Helper to help simplify my masterpages menu, however it is not rendering on the HTML when I use it.. I'm thinking I will need to create a partial view, any ideas?
I did this..
public static string CreateAdminMenuLink(this HtmlHelper helper, string caption, string link)
{
var lnk = TagBuilder("a");
lnk.SetInnerText(caption);
lnk.MergeAttribute("href", target);
return lnk.ToString(TagRenderMode.SelfClosing);
}
Now in my View, i have
<% Html.CreateAdminMenuLink("Home", "~/Page/Home"); %>
Thanks: Dave Swersky
Fix was: I forgot the equals and removed the semi-colon
<%= Html.CreateAdminMenuLink("Home", "~/Page/Home") %>
but when I look at the source, its empty.. tried adding <% using (Html.BeginForm()) %> and it adds a form.. but the link still doesnt come up.. debugged and the string works when i look at the watch, but does not render..
Any ideas?
Modify your markup:
<%= Html.CreateAdminMenuLink("Home", "~/Page/Home") %>
The equals sign and no semicolon should do the trick.

Resources