rails 中的文章统计小部件 [英] Article stats widget in rails
问题描述
我一直在尝试为我的 rails 应用程序设计一个小部件,但不幸的是我发现自己缺乏 Javascript 技能...
我能够根据此博客中的简短指南创建一个小部件:http://www.eduvoyage.com/2008/8/3/widget-with-rails
但我需要的和他们描述的有点不同.
如果您有一个类似 digg 或 tweetmeme 的应用程序,可以为特定文章投票,您如何创建一个小部件来显示投票?
假设文章模型具有名称、链接和投票计数器.
这是我试过的:
在文章控制器中:
定义索引@article = Article.find_by_link("#{request.url}")response_to do |格式|格式.js格式.html结尾结尾
在 index.js.erb 中:
var txt = ''txt += "";txt += "<h2>文章投票数:</h2>";txt += "<%= escape_javascript(@article.votes) %>";txt += "</div>";文件.写(txt);我遇到的问题是@article 结果为零.为了弄清楚出了什么问题,我将这段代码添加到我的 javascript 文件中:
txt += "<%= escape_javascript(request.url) %>";
屏幕上的结果是:http://localhost:3000/articles.js.显然,当 find_by_link 方法只返回 javascript 文件的路径时,我将无法使用它.find_by_link 方法中我想要的是浏览器中的当前 url.
或者有更好的方法来做到这一点?
有没有办法让我的 rails 应用程序确切知道引用的是哪篇文章?
解决方案 许多小部件实际上运行两段代码: 像这样:
客户站点上的小部件代码:
<script src="http://yourdomain.com/javascripts/widget.js" type="text/javascript"></script>
widget.js
:
(function(){var loc = window.location,标题 = 文档.标题,href = 'http://yourdomain.com/widgets/count.js?url=' + encodeURIComponent( loc );href = href + '&title=' + encodeURIComponent( title );var script = document.createElement('script');script.src = href;script.type = "text/javascript";//调用你的真实脚本:document.documentElement.firstChild.appendChild( script );})();
然后您可以在 Rails 中使用 params[:url]
和 params[:title]
访问它这不是一个完整的解决方案,因为您的第二个脚本没有关于放置小部件的位置的上下文.大多数大的(digg、tweetmeme)使用 document.write
但我个人不喜欢使用它.
如果我正在构建这整个东西,我可能会有一个空的 div,其中 id 是您拥有用户副本的小部件代码的一部分,或者我可能会在第一个中使用 document.write
js
文件写出第二个脚本可以通过 getElementById
填充的保持 div
.
最后一句警告:请务必在匹配前清理 URL.您不希望 http://domain.com
和 http://domain.com/
提供两个不同的匹配项.
I've been trying to design a widget for my rails app and unfortunately find myself lacking in Javascript skills...
I was able to create a widget based on the short guide from this blog: http://www.eduvoyage.com/2008/8/3/widget-with-rails
But what I need is a little different from what they describe.
If you had a digg or tweetmeme-like application with votes for a particular article, how could you create a widget to display the votes?
Let's say that an article model has a name, a link and a votes_counter.
This is what I tried:
In the articles controller:
def index
@article = Article.find_by_link("#{request.url}")
respond_to do |format|
format.js
format.html
end
end
In index.js.erb:
var txt = ''
txt += "<div id='article_widget'>";
txt += "<h2>Article vote count:</h2>";
txt += "<%= escape_javascript(@article.votes) %>";
txt += "</div>";
document.write(txt);
The problem I'm getting is that the @article turns out to be nil. To figured out what was wrong I added this bit of code to my javascript file:
txt += "<%= escape_javascript(request.url) %>";
The result on the screen was: http://localhost:3000/articles.js. Obviously I wouldn't be able to use the find_by_link method when it just returns the path of the javascript file. What I'd like in the find_by_link method is the current url in the browser.
Or is there a better way to do this?
Is there a way to let my rails app know exactly which article is being referenced?
解决方案 Many widgets actually run two pieces of code: Something like this:
Widget code on client site:
<script src="http://yourdomain.com/javascripts/widget.js" type="text/javascript"></script>
widget.js
:
(function(){
var loc = window.location,
title = document.title,
href = 'http://yourdomain.com/widgets/count.js?url=' + encodeURIComponent( loc );
href = href + '&title=' + encodeURIComponent( title );
var script = document.createElement('script');
script.src = href;
script.type = "text/javascript";
// Call your real script:
document.documentElement.firstChild.appendChild( script );
})();
You could then access it in Rails by using params[:url]
and params[:title]
This is not a complete solution as your second script would not have context as to where to place the widget. Most of the big ones (digg, tweetmeme) use document.write
but I have a personal aversion to using it.
If I were building this whole thing, I would probably have an empty div with an id be part of the widget code you have the user copy, or I might use document.write
in the first js
file to write out a holding div
that the second script can populate via getElementById
.
One final word of warning: Be sure to clean the URL before matching. You don't want http://domain.com
and http://domain.com/
providing two different matches.
这篇关于rails 中的文章统计小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文