2013-08-19 82 views
1

我在導軌mvc上使用紅寶石,我試圖使用ajax調用來發送一些數據回服務器端。我對如何在mvc中發出和處理ajax請求感到困惑。ajax調用導致內部服務器錯誤500導軌控制器

----------------在我的JavaScript文件------------------

} );

我使用的respond_to塊捕獲在我的控制器這個請求:

----------------在我家控制器------- -----------

respond_to do |format| 
    format.js { 
     render 'users/locations' 
    } 
    format.html {} 
end 

---------------- route.rb ------------- -----------

post 'users/locations' 
get 'users/locations' 
match "/locations", to: "users#locations" 

我想這種方式提取數據:

----------------在我的位置action ------------------

dasdasdd // used as a breakpoint to test whether this part was executed. it wasn't. 
current_user.update_attribute :Latitude, params[:lat] 
current_user.update_attribute :Longitude, params[:lng] 

隨着調試firefox控制檯,我看到內部服務器錯誤500.這可能解釋爲什麼位置操作從未執行。但爲什麼會出現錯誤500?任何人都可以照亮我可能會怎樣錯的?

+0

你可以找到大量的信息在'應用程序/ development.log '以及關於異常的'500'錯誤的響應主體。我們只是猜測你的問題是什麼,而不知道* Exception拋出的是什麼。 – deefour

回答

0

既然你已經在你的Ajax調用指定的數據類型是JSON,嘗試

format.json { 
     render 'users/locations' 
    } 

,而不是format.js在控制器

+0

非常感謝,但我解決了這個謎。這是我使用的設計認證模塊的問題。當位置操作中該模塊失敗時,方法不起作用,導致500內部服務器錯誤。爲了驗證目的,ajax帖子顯然需要一個csrf頭文件。 http://stackoverflow.com/questions/7393871/devise-not-setting-current-user-on-ajax-post-even-though-correct-x-csrf-token-is – Feverbeaver

相關問題