简体   繁体   中英

Using qTip2 with Ruby on Rails

I found this wonderfull tooltip called qTip2. Loving it so far but has gotten myself into a problem using it.

I was able to install and get qTip2 running with my Rails 3.1 setup. However I'm running into a problem when I'm using qTip2's ajax functionality.

qTip2 ajax request requires a link to the script to be executed before displaying it in the tooltip content div (which is automatically generated by qTip2). You can look at the code I'm referring to here.

http://www.craigsworks.com/projects/qtip2/demos/#ajax

The problem with it is that it does not really go well with RoR. An ajax request declared using RoR is more explicit, where I can explicitly tell rails which div id I would like to update.

Given the following circumstances I needed to do the following things:

  1. the tooltip content must be dynamic
  2. I needed to use the qTip2 ajax functionality so that I could create a custom layout for the tooltip content.

I did try the followings though:

  1. Put the html snippet to be loaded to the content div in the public folder. The content loaded with the right format but I can't have dynamic content. html.erb files didn't seem to work in this folder. Is there any alternative to this folder that would work but with dynamic content?

  2. Tried to use qTip2 ajax request with RoR but was not successful. What I did was I tried to explicitly define the ajax update id in the .js respond file (going through the controller and views etc.). But figuring out the exact id that was generated by qTip2 plugin was a little too overwhelming for me.

here is what I have

html

 <a id="editor1" href="/deals_details.html.erb">

javascript

$(document).ready(function()
{

   $('#editor1').each(function()
   {
      $(this).qtip(
      {
         content: {
            text: '<img class="throbber" src="/assets/throbber.gif" alt="Loading..." />',
            ajax: {
               /*once: false*/
               url: $(this).attr('href') 

            }, 
            title: {
               text: 'qTip2 Test', 
               button: true
            }
         },
         position: {
            at: 'center', 
            my: 'center',
            viewport: $(window), 
         },
         show: {
            event: 'click',
            solo: true 
         },
         hide: 'unfocus',
         style: {
            classes: 'ui-tooltip-wiki ui-tooltip-light ui-tooltip-shadow'
         }
      })
   })

   // Make sure it doesn't follow the link when we click it
   .click(function(event) { event.preventDefault(); });
});

CSS

.ui-tooltip-wiki{
   max-width: 440px;
}

   .ui-tooltip-wiki .ui-tooltip-content{
      padding: 10px;

      line-height: 12.5px;
   }

   .ui-tooltip-wiki h1{
      margin: 0 0 7px;

      font-size: 1.5em;
      line-height: 1em;
   }

   .ui-tooltip-wiki img{ padding: 0 10px 0 0; }

   .ui-tooltip-wiki p{ margin-bottom: 9px; }
   .ui-tooltip-wiki .note{ margin-bottom: 0; font-style: italic; color: #888; }

Please help me!! :) Apology if this post looks stupid, this is my first question on stackoverflow.

I think you're making it too complicated. The qTip ajax functionality simply requests a URL, and the return value is HTML content within a div tag. If you look at the source of the demo you mentioned, you'll see a request for http://www.craigsworks.com/projects/qtip2/data/burrowingowl.php . If you look at what that produces, it is simply a div tag with some content (the style looks different because it is further transformed by the page's CSS).

So the question becomes: can you provide your dynamic content in a div in response to a URL in Rails? Of course! Perhaps you want to set up a SnippetController with a show action and then using pretty standard Rails conventions/routes your URL might be /snippet/show/[ID] and you simply have the view produce the required div content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM