I am attempting to automatically refresh a specific partial view every 60 seconds on the homepage. To make it easier to manage, I have divided the actions into two routes. However, I am encountering an issue with the respond_to block and could use some assistance in finding a better approach.
feed_controller.rb
def index
@hashtags = hashtag_refresh
end
def hashtag_refresh
Hashtag.order('created_at DESC').limit(10).uniq
respond_to do |format|
format.js
end
end
feed\hashtag_refresh.js.erb
$('.trends_li_container').html("<%= escape_javascript(render('feed/shared/hashtag_list')).html_safe %>");
routes.rb
get 'feed/hashtag_refresh', to: 'feed#hashtag_refresh'
hashtag_autorefresh.js
//Refresh Hashtags Partial
$(document).ready(function (){
setTimeout(refreshHashtag, 60000)
});
//Calls action refreshing the partial
function refreshHashtag() {
$.ajax({
url: 'feed/hashtag_refresh',
type: 'GET',
dataType: 'script'
})
}
feed/shared/ folder _hashtag_list.html.erb
feed/ controller folder hashtag_refresh.js.erb
Server Development Log
Started GET "/feed/hashtag_refresh?_=1462210930323" for 127.0.0.1 at 2016-05-02 13:45:05 -0400
Processing by FeedController#hashtag_refresh as JS
Parameters: {"_"=>"1462210930323"}
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 5]]
Rendered feed/shared/_hashtag_list.html.erb (0.0ms)
Rendered feed/hashtag_refresh.js.erb (11.5ms)
Completed 200 OK in 50ms (Views: 47.5ms | ActiveRecord: 0.0ms)