2012-05-24 49 views
-1

這可能是一個初學者的問題(我是Rails 3的新手),但我嘗試使用Ajax,一旦用戶點擊一個鏈接,發起一個$ .post調用,並做一些改變在服務器端。使用帶軌道的Ajax

在_share部分,我有:

%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 

功能SubmitwithAjax()是在文件的application.js:

function submitWithAjax() { 
$.post('background_forward', $(this).serialize(), null, "script"); 
    return false; } 

在我的路線,我有:

match 'background_forward', :to => "branches#background_forward" 

我只需要將submitwithAjax函數轉到我的服務器中的特定方法。希望得到一些幫助。謝謝!

+0

http://railscasts.com/episodes/205-unobtrusive-javascript – kreek

回答

1

你的js到目前爲止是好的,現在你只需要在BranchesController上添加正確的動作來響應POST,並且在你的routes.rb(我相信這是一個默認的GET)中註冊該方法。

+0

好吧,所以我設法得到我想要在控制器內的方法,但現在它引發了500內部服務器錯誤,因爲我沒有指定一個迴應格式。問題是,我不想有一個響應格式,我只是希望它什麼都不做。它給出了錯誤:ActionView :: MissingTemplate(缺少模板分支/ background_forward,應用程序/ background_forward {:locale => [:en],::formats => [:html],:handlers => [:erb,:builder, :coffee,:jbuilder,:haml]}。搜索: –

+0

render:layout => false – Ron

+0

非常感謝!現在有道理。 –