This might be a beginner question (I'm new to Rails 3), but I'm trying to use Ajax once a user clicks on a link, initiate a $.post call, and do some changes in the server-side.
In the _share partial, I have:
%a.fbbutton.twitterbutton{:id => "twitter"} (href="javascript:window.open(%22http://twitter.com/share?text=Does anyone know a #{@branch.title} @branchly%21&url=http%3A%2F%2Fwww.branch.ly/branches/#{@branch.id}%22,%22Share%22,%22width=500,height=250,scrollbars=yes%22)" class="twitter button" onclick="submitWithAjax();") Tweet
The function SubmitwithAjax() is in application.js file:
function submitWithAjax() {
$.post('background_forward', $(this).serialize(), null, "script");
return false; }
In my routes I have:
match 'background_forward', :to => "branches#background_forward"
I just need to have the submitwithAjax function go to a specific method in my server. Would appreciate some help. Thanks!
Your js is fine so far, now you just need to add the correct action on the BranchesController
that responds to a POST, as well as registering that method in your routes.rb (I believe it's a GET by default).