Rails tag helper - ruby-on-rails

I want to do
<meta http-equiv="refresh" content="0;URL='my_url" />
with rails tag helper
i'm doing like this
tag(:meta, 'http-equiv' => (:refresh),:content=>("0;URL=#{my_url}"))
but i getting
"<meta content=\"0;URL=my_url" http-equiv=\"refresh\" />"
where is my mistake?

Related

How to get content value out of meta tag in ruby on rails?

I have this list of meta tags in my view HTML (after the page loads). The tag is generated dynamically,
#meta = "\n <meta content=\content1\">\n <meta content=\content2\">\n <meta content='content2\">\n ....... <meta content=\"2019/01/10 09:59:59 +0900\" name=\"r_end\">\n \n"
I wanted to fetch the value 2019/01/10 09:59:59 +0900 inside content i.e.<meta content=\"2019/01/10 09:59:59 +0900\" name=\"r_end\">. Is there a way to get the value of the content from the meta tag.
Given a #meta variable containing some HTML snippet as a string:
#meta = <<-HTML
<meta name="foo" content="content1">
<meta name="bar" content="content2">
<meta content="2019/01/10 09:59:59 +0900" name="r_end">
HTML
You can use Nokogiri to parse it:
require 'nokogiri'
doc = Nokogiri::HTML::DocumentFragment.parse(#meta)
doc.at_css('meta[name="r_end"]')['content']
#=> "2019/01/10 09:59:59 +0900"
at_css returns the first element matching the given CSS selector and [] returns the value for the given attribute.
How about a using simple regular expression to capture the value using String#scan.
This will work only if the name of metatag doesn't change
#meta = "\n <meta content=\content1\">\n <meta content=\content2\">\n <meta content='content2\">\n ....... <meta content=\"2019/01/10 09:59:59 +0900\" name=\"r_end\">\n \n"
#meta.scan(/content=\"(.*)\" name=\"r_end\"/)
#=> [["2019/01/10 09:59:59 +0900"]]
Explanation:
The above code will capture the value of content with metatag name="r_end"
If you think there might be some other HTML elements with name="r_end" you might need to add some other identifier in the regex

og meta Facebook & rails index: sharing info not updated in facebook share-box but correct in html

I'm using Facebook open graph meta tag inside one of my rails index view, which is an article post including a share on facebook link.
Everything worked well for the first post, and the facebook share box takes into account the meta provided (title, url, etc).
But when I want to share another post, then the facebook share-box keep the previous post info. In the html, the meta are good, and when I use facebook debug, everything is fine.
What I am missing ? There is a cache flush to do ?
Thank you a lot for the help
<meta property="og:title" content="<%= #post.title %> wanted on XXX!" />
<meta property="og:site_name" content= "Site Name" />
<meta property="og:url" content="http://siteroot<%= display_path(#post) %>" />
<meta property="og:description" content="Site is bla bla bla" />
<meta property="og:image" content="<%= #post.vignette.url(:medium) %>" />
<meta property="og:type" content="article" />
<meta property="article:publisher" content="https://www.facebook.com/appname" />
<meta property="article:section" content="Responsive" />
I can't tell if you're doing this or not but make sure you're sharing the post page and not the index page.
href="www.facebook.com/sharer/sharer.php?u=<%= #post.url %>"
The og tags in the head won't update without reloading the page, so you'll have to point Facebook to each post's unique url.

Razor - #Html.Raw() still encoding & in meta tag attributes

When I use #Html.Raw(mystring) normal it renders properly example:
#{ ViewBag.Title = "My Site®"; }
<title>#Html.Raw(ViewBag.Title)</title>
Will properly render <title>My Site®</title> but when I use it in an attribute:
<meta name="description" content="#Html.Raw(ViewBag.Title)" />
It renders <meta name="description" content="My Site&reg;" /> which is not correct because then it will not render the registered mark.
How do you correct this behavior?
As patridge pointed out in his answer you can just include the attribute markup in the parameter to .Raw.
In your case that would result in something similar to the following:
<meta name="description" #Html.Raw("content=\"My Site&reg;\"") />
You can do this in your _layout.cshtml:
<title>#{var title = new HtmlString(ViewBag.Title);}#title</title>
This worked for me.
I think it's the tip-top solution:
#{ ViewBag.Title = "My Site®"; }
<title>#Html.Raw(ViewBag.Title)</title>
<meta name="description" content="#HttpUtility.HtmlDecode(ViewBag.Title)" />

Hpricot search all the tags under one specific namespace

For example I have the following code:
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><io:content part="title" /></title>
<link rel="icon" href="/document/7e9f29e2-cdee-4f85-ba25-132fa867aa90/latest" type="image/x-icon" />
<n1:content description="Standard CSS" uuid="d069071c-3534-4945-9fb6-2d7be35a165e" />
<n1:term>Content Development</n1:term>
</head>
This XHTML snippet is not strictly legal because there is no namespace declared before so I cannot use Nokogiri which has better namespace support.
I want to do a single search that can find both the node <n1:content> and <n1:term> and all the tags under 'n1' namespace.
How to achieve that? Thanks!
It looks like Hpricot does not handle namespaces that fully.
You can select if you know the element regardless of prefix:
doc.search("title")
=> #<Hpricot::Elements[{elem <title> {emptyelem <io:content part="title">} </title>}]>
... but this is not what you asked.
Here's my hack workaround: find all namespace elements using regex first, then search for those using Hpricot:
elems = doc.to_s.scan(/<\s*(n1:\w+)/).uniq.join("|")
=> "n1:content|n1:term"
doc.search(elems)
=> #<Hpricot::Elements[{emptyelem <n1:content description="Standard CSS" uuid="d069071c-3534-4945-9fb6-2d7be35a165e">}, {elem <n1:term> "Content Development" </n1:term>}]>

Element 'title' occurs too few times, XHTML validation warning in ASP.NET.MVC master page

I am getting the following XHTML validation warning in my ASP.NET MVC master page:
Validation (XHTML 1.0 Transitional): Element 'title' occurs too few times.
The title tag for the master page is included in the ContentPlaceHolder in the head tag as shown in the code below. The title tag in the ContentPlaceHolder is not taken into account when performing the validation, and I do not want to just add another one in the head tag because then I will be left with two title tags.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<asp:ContentPlaceHolder ID="head" runat="server">
<title></title>
</asp:ContentPlaceHolder>
</head>
One work around that I have found is to use the following technique in the head tag:
<% if (false) { %>
<title></title>
<% } %>
Is this the best practice to resolve this warning? I am not a huge fan of adding the excess code just to pass validation warnings but I will live with it if there is not a better alternative.
Do this instead:
<head>
<title><asp:ContentPlaceHolder ID="title" runat="server">Default Page Title Here</asp:ContentPlaceHolder></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>
Or as an alternate, set the title programattically from each page.
What's happening in your case is that when a new view is created, it creates empty content items which override the default content in the placeholders. If you remove the empty content blocks from the view, the default placeholder content will be used, but then you can't set the contents from the view. Using the code above you can override a default title from each view and include scripts, etc. in the head independently of each other.
Here possible solutions are
First solution is
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<asp:ContentPlaceHolder ID="head" runat="server">
//<title></title> - this line should be removed.
</asp:ContentPlaceHolder>
second solution is,
Check whether the head tag having attribute runat="server",if have not set runat prperty means not a problem else need to remove the runat tag.

Resources