Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 481121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:57:45+00:00 2026-05-13T00:57:45+00:00

I’ve been trying to design a widget for my rails app and unfortunately find

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:57:45+00:00Added an answer on May 13, 2026 at 12:57 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.