这段代码是从一个视图中执行的,它可以工作:
<% @metaTags = OpenGraph.fetch('http://www.someurl.com') || nil %>
<% if @metaTags != nil %>
postTitle = truncateStr('<%= @metaTags.title %>', 72);
<% end %>
我想做同样的事情,但http://www.someurl.com
作为 javascript 计算的参数传递,就像这样
尝试 1
var someURL = ... (compute value);
setCookie("someURL", someURL, 10000);
<% @metaTags = OpenGraph.fetch(cookies[:someURL]) || nil %>
<% if @metaTags != nil %>
postTitle = truncateStr('<%= @metaTags.title %>', 72);
<% end %>
它不起作用。
尝试 2
var someURL = ... (compute value);
setCookie("someURL", someURL, 10000);
<% readPostURL %>
<% if @metaTags != nil %>
postTitle = truncateStr('<%= @metaTags.title %>', 72);
<% end %>
并在控制器中
private
def readPostURL
@metaTags = OpenGraph.fetch(cookies[:postURL]) || nil
end
helper_method :readPostURL
它也不起作用。
这两种情况似乎都对 cookie 有问题。有没有办法使用一些 javascript 从视图中运行 OpenGraph.fetch(parameter) variable
?