Hi all,
I'm doing my best to create a site badge or widget for my site, which is a rails site.
Essentially it's a site where users post content that they've created and other who like that content can make a donation to express their appreciation for it.
I asked a similar question a while back about how to create a widget, but that was before I had done any real study of Javascript.
The first script (in localhost:3000/javascripts/widget.js) worked fine I think:
var Widget =
{
init:function()
{
var loc = window.location;
var title = document.title;
var href = 'http://localhost:3000/donations/new.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);
}
};
Widget.init();
(The reason why I entitled it new.js was because that's where all the statistics for the link are displayed on the main site) To test this script I made a simple HTML file to call it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test Widget</title>
<script src="http://localhost:3000/javascripts/widget.js" type="text/javascript"></script>
</head>
<body>
<h1>Test Widget</h1>
</body>
</html>
When the above script runs I checked firebug and sure enough it calls the second script:
<script src="http://localhost:3000/donations/new.js?url=file%3A%2F%2F%2FC%3A%2Fgoldhat_production%2Ftest_widget.html&amp;title=Test%20Widget" type="text/javascript"></script>
Finally I created a new.js.erb file with the following script just to make sure that it was being called:
var Stats =
{
init: function()
{
alert("Hello World!");
}
};
Stats.init();
And in the donations controller I added the following code to the "new" action:
respond_to do |format|
format.js
format.html
end
So I gave it a whirl and I don't get a Hello World alert when I refresh the page but when I get what is actually being called I get this error (truncated):
26<h1>
27 ActionController::MethodNotAllowed
28
29</h1>
30<pre>Only put requests are allowed.</pre>
31
32
33
34<p><code>RAILS_ROOT: c:/goldhat_production</code></p>
35
36<div id="traces">
There seems to be a lot of places where I could go wrong with this, I'm not sure where, though.