I'm trying to refresh the browser after the user clicks a link to an action that updates the database.
I want to avoid using redirect_back to prevent being sent back to the top of the screen after reload so I'm adding a JS click listener to the link_to tag with a code like this:
# in my view
<h5 class='my-link'><%= link_to 'vote', vote_path' %></h5>
# at the bottom of my application layout
<script>
$('.my-link').click(function (event) {
window.location.reload(true);
});
</script>
<script>
$(document).ready(function() {
$('.puke-icon, .clean-link, .comment-send').click(function() {
setTimeout(function(){
window.location.reload(true); },
50);
});
});
</script>
In your vote controller action
, use location.reload()
in respond_to do format
.
respond_to do |format|
format.js {render inline: "location.reload();" }
end
View:
<h5 class='my-link'><%= link_to 'vote', vote_path' %></h5>
Controller:
class VotesController < ApplicationController
def vote
----------------------
// your code goes here
----------------------
respond_to do |format|
format.js {render inline: "location.reload();" }
end
end
end
This will reload the page after the database action is completed.