How to write <noscript> tag in html.erb file - ruby-on-rails

I have an app.html.erb and I need to add noscript tag in it. Like for adding a script tag we do something like
<%= javascript_tag do %>
<% end %>
I am not able to figureout how to write noscript tag in html.erb. Please help me.
<noscript>
<iframe src="googletagmanager.com/ns.html?id=#{key}" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>

I think this should work:
<noscript>
<iframe src="googletagmanager.com/ns.html?id=<%= key %>" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
ERb will interpret things in <%= %> as ruby, so this would asume there's a variable key that was passed into the erb.

Related

How ignore empty response in ruby view loop

as i'm new to ruby and rails in general i would have a short question cause i'm stuck at a minor issue.
I'm calling a content API from my controller and looping thru the response in the view directly. The main problem is: If one of the objects, has an empty value which i'm calling..it breaks the view.
Question would be how can i scip the elements which are emtpy...in example if post["heroimage"]["url"] is empty?
Example view:
<div class="gallery">
<% #blog.each do |post| %>
<a target="_blank" href="blog/<%= post["id"] %>">
<img src="<%= #host + post["heroimage"]["url"]%>" alt="" width="600" height="400">
</a>
<div class="desc"><%= post['description'] %></div>
<% end %>
</div>
There is nothing Rails or Rails views specific about your question. What you're trying to do is skip elements in an each loop in Ruby, and the answer is, next
somethings.each do |thing|
next if thing.nil?
# .. does not get called if thing is nil
end
From what I understand from the question and your comments you can use,
post.dig("heroimage", "url")
here is the link to dig method documentation. If you want to skip the image in case of empty url you can do something like this
<div class="gallery">
<% #blog.each do |post| %>
<a target="_blank" href="blog/<%= post["id"] %>">
<% if post.dig("heroimage", "url") %>
<img src="<%= #host + post["heroimage"]["url"]%>" alt="" width="600" height="400">
<% end %>
</a>
<div class="desc"><%= post['description'] %></div>
<% end %>
</div>
This will still show the title even if the image URL is empty.

How do I use image_tag in <img class="rounded-circle">

I'm trying to use the code below to display a user avatar picture. This picture can be accessed by using <%= image_tag(#user.avatar) %>
I'm unsure of how to replace the src= in the code below because previous attempts to replace it ended in failure.
<img class="rounded-circle" width="45" src="https://picsum.photos/50/50" alt="">
Previously I use to used <%= image_tag(#user.avatar, style: 'width:50%') %>
to show images, but I prefer the styling of the <img class> more. If anybody can help we out with this on point me into the direction of some documention on this I would greatly appreciate it
I believe this would work:
<%= image_tag(#user.avatar, class: 'rounded-circle') %>
Try to use image_tag like this
<%= image_tag #user.avatar.url, class: "rounded-circle" ,alt: ""%>
<style type="text/css">
.rounded-circle{
width: 45px;
}
</style>
<img class="rounded-circle" width="45" <%=image_tag("No_image.jpg")%>
This ended up working for me. Seems like when I was trying to do it at first I was using the <% instead of <%= user error is the worst! Thanks for the help everybody!

add an image tag in a div

I making a blog part with a buyed template.
I'm looking for add image in div. Something like that.
<div class="card card-raised card-background" style="background-image: url('<%= image_tag blog.couverture.url(:hd) %>')">
<div class="card-body">
...
Unfortunatly it's not working. So I looking for help after one hour of research on internet.
Thank you for you help.
Your image must be in "assets/images" folder on your rails proyect, then try this:
<div>
<img src="<%= asset_path 'image_name.format' %>" alt="">
</div>

Rails: show images without image tag

I want to show images without using <%= img_tag %>. So I tried following things
<img src="/images/bg/1.jpg" >
<img src="assets/images/bg/1.jpg" >
<img src="assets/bg/1.jpg" >
<img src="bg/1.jpg">
<img src="/bg/1.jpg">
This does not show the image. But if I do this
<%= image_tag("bg/1.jpg") %>
This works. But why it does not work with img tag. How can I make this work?
My image location app/assets/images/bg/1.jpg
Note:
<%= image_tag("bg/1.jpg") %>
outputs
<img src="/assets/bg/1-ed0a6edfcc008f66c22373e3d57a5c01.jpg" >
generated image link is not exactly the original image name
You need to generate link through image_url if you really want to use the plain img tag instead of fancy image_tag by Rails, and here is how:
<img src="<%= image_url('bg/1.jpg') %>">

Rails html_safe is changing my image paths

I have a string of html in a rails helper module:
def app_link
"<a href=\"https://itunes.apple.com/us/app/one-spark/id630800549?mt=8\" target=\"_blank\">
<img src=\"/assets/apple-download-button.png\" alt=\"App Store\" />
</a>"
end
When it renders normally it escapes the HTML, but when I add
<%= app_link.html_safe %>
The assets part of the image path is dropped and changed to:
<a href="https://itunes.apple.com/us/app/one-spark/id630800549?mt=8" target="_blank">
<img src="apple-download-button.png" alt="App Store" />
</a>
I'm guessing this has something to do with the asset pipeline but it seems like really bizarre behaviour.
Try with this modified version of your helper. Hope it helps.
def app_link
app_link_html = ''
app_link_html << "<img src=\"/assets/apple-download-button.png\" alt=\"App Store\" />"
app_link_html.html_safe
end
and in view
<%= app_link %>

Resources