开发者

Creating a site badge or widget in rails: Problem calling the second script

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:/gol开发者_开发知识库dhat_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.


Your routes.rb file is only allowing put requests to your new action. New should allow get if you are using map.resources, but it appears you are not using that functionality.

I see you have three options:

1) If you are going to be using rjs, then move the javascript call to a different method that isn't already being used and set up the route to that action if necessary.

map.connect '/donations/[action_name]', :controller => 'donations', :action => '[action_name]', :conditions => { :method => :get }

2) If you are not going to be using rjs, just move it to static resources. Just move the file to your /public/javascripts directory and link to it from there.

3) allow get requests to your new method, by adding this to your routes.rb. This might be just allowing the action in your existing route statement, depending on what is already there.

map.connect '/donations/new', :controller => :donations, :action => :new, :conditions => { :method => :get }

I would personally go with option 1 or 2, just because I don't see the relationship to "new", but I likely don't see the full picture.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜