javascript - refreshing a div element using jQuery in Rails 3 app -



javascript - refreshing a div element using jQuery in Rails 3 app -

i have rails 3 app uses resque run long running jobs in background , want create view results on page

at moment have scenario working

resque runs job , writes info database (updates info entry) progresses. 1 time job finish updates 1 of database entries i.e. sets finish = 1 (complete created value of 0)

db schema is

create_table "refreshes", :force => true |t| t.string "name" t.string "info" t.integer "complete" t.datetime "created_at" t.datetime "updated_at" end

another page view in app display database entry calling it’s controller , displaying latest info on page, view has piece of logic insert standard javascript refresh element if finish value in database not equal 1

view.html.erb

<% if @refreshes.complete == 0 %> <meta http-equiv='refresh' content='5'> <%end%> <div id="data"> <p> <b>name:</b> <%= @refreshes.name %> </p> <p> <b>info:</b> <%= @refreshes.info %> </p> </div>

controller method

def view @refreshes = refresh..find(params[:id]) respond_to |format| format.html # view.html.erb format.xml { render :xml => @refreshes } end end

this works fine total page reload every 5 seconds when finish set 0, , want refresh div displays info ( div id = data) without refreshing whole page.

i think can done using jquery , javascript can’t find way can help? or if can point me in direction appreciate cheers mike

yes, can done using javascript. 1 approach be

to refresh div every n minutes, can utilize setinterval method phone call function every n miliseconds. this function should create ajax request info server , insert info in html.

below code uses jquery can utilize other library result.

$(document).ready(function() { // info every 5 mins , refresh mydiv var pingafter = 5000; var refreshid = setinterval(function() { $("#mydiv").load('/partial/data'); }, pingafter); });

brightcherry.co.uk site has illustration code accomplish same.

javascript jquery ruby-on-rails-3

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -