Jquery & Rails 3: Run controller action from js file? -
i click div on page run number of javascript tasks, run action on controller. explain in greater detail:
i have untitled.js file in have put bunch of jquery code :
$(function(){ $('nav li').hover(function(){ $(this).addclass("bigger", 50); }, function(){ $(this).removeclass("bigger", 50); }); $('#notify').click(function(){ $('.notifications').show(); }); $('#notify').click(function(){ (some code run controller action) }); });
i run controller action called "run_tasks" on "users" controller when #notify button clicked, showing notifications class. can see in above code have jquery code showing .notifications sorted out, don't know put in "(some code run controller action)". provide information controller action, run tasks want run, doesn't redirect new page or anything. here basic format of users controller:
class userscontroller < applicationcontroller .... def run_tasks end end
is trying possible, , if jquery code put in there? i'm using rails 3.0.9 , newest jquery.
as long have route controller action, can cause run issuing ajax request jquery's $.get().
$('#notify').click(function(){ $('.notifications').show(); $.get('/users/run_tasks'); });
there's no need 2 $('#notify').click()
either, can put 2 statements in 1 block (as have done in code snippet above).
note in case .get()
makes request call controller action on server , ignores result, can use handle data returned controller. refer doc link above.
Comments
Post a Comment