link_to action è creare o indicizzare, a seconda di dove si trova il codice
-
07-07-2019 - |
Domanda
Voglio scorrere ogni pagina dell'indice di un modello impaginato. Sto usando jquery per scorrere le pagine. Trovo che l'espressione link_to stia chiamando l'azione create quando viene chiamata da jquery. Le espressioni link_to chiama l'azione indice in qualsiasi altra circostanza.
Ad esempio:
<div id="links">
<% 1.upto(@total_pages) do |number|%>
<%= link_to 'Page',"#{clients_path}?page=#{number}" %>
<br />
<% end %>
</div>
Produce un collegamento che chiama l'azione indice, come ci si aspetterebbe dal routing predefinito (ad esempio map.resources: client).
Per scorrere le pagine ho il seguente 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>
Ed ecco il 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();
});
L'azione risultante da questa è una chiamata da creare. Cosa mi sto perdendo?
Soluzione
Sta succedendo perché stai postando nell'URL, $ .post (...). Rails vede il post e cerca di gestirlo con la sua magia di risorse mappata. Cambialo in $ .get (...) e dovresti andare bene.