Question

I want to cycle through each page of a paginated model's index. I'm using jquery to cycle through the pages. I find the link_to expression is calling the create action when called by jquery. The link_to expressions calls the index action under any other circumstances.

For instance:

<div id="links">
  <% 1.upto(@total_pages) do |number|%>
    <%= link_to 'Page',"#{clients_path}?page=#{number}" %> 
    <br />
  <% end %>
</div>

Produces a link that calls the index action, like you would expect with default routing (i.e. map.resources :clients).

For cycling through the pages I have the following html:

<div id="show" style="display: none">
    <% 1.upto(@total_pages) do |number|%>
      <%= link_to 'Page#{number}',"#{clients_path}?page=#{number}" %> 
    <% end %>
</div>
<a id="stop" href="#" onclick="stop()">stop</a>
<a id="start" href="#" onclick="start()" style="display: none">start</a>
<div id="output"></div>

And here's the jquery:

var cur = 0;
var links = [];
var timeout;

function nextLink() {
    if (cur + 1 >= links.length) {
        cur = 0;
    } else {
        cur++;
    }
    return links[cur];
}

function nextPage() {
    $.post(nextLink(), function(data) {
        $("#output").fadeOut("slow", function(){
            $("#output").html(data);
            $("#output").fadeIn("slow");
        });
    });
}

function stop() {
    clearTimeout(t);
    $("#stop").hide();
    $("#start").show();
}

function start() {
    $("#start").hide();
    $("#stop").show();
    slideshow();
}

function slideshow() {
    nextPage();
    t = setTimeout("slideshow()", 5000);
}

$(document).ready(function() {
    $("#show").children().each(function() {
        links.push(this);
    });

    slideshow();
});

The resulting action from this is a call to create. What am I missing?

Was it helpful?

Solution

It's happening because you're Posting to the url, $.post(...). Rails sees the post and tries to handle it with it's mapped resource magic. Change that to $.get(...) and you should be fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top