0
我想對通過腳手架生成的'create'路由執行POST請求。 控制器名稱是'翻譯',創建路徑是'/翻譯'。爲了做到這一點,我使用jQuery創建了一個ajax請求。我的請求看起來像 -jQuery,Rails和Ajax POST到數據庫
$.ajax({
type: "POST",
url: "/translations",
data: ?,
success: function(msg){
alert(msg);
}
});
我想要數據包含我的參數,以便生成的翻譯將具有這些值。翻譯的參數是'源'和'輸出'。 腳手架生成,翻譯器「創造」的方法看起來像 -
def create
@translation = Translation.new(params[:translation])
respond_to do |format|
if @translation.save
format.html { redirect_to(@translation, :notice => 'Translation was successfully created.') }
format.xml { render :xml => @translation, :status => :created, :location => @translation }
else
format.html { render :action => "new" }
format.xml { render :xml => @translation.errors, :status => :unprocessable_entity }
end
end
end
我試圖插入不同值的數據,但它並不適用於他們。新的翻譯被創建,但沒有參數。 這些都是我想,以使「輸出」值「你好」至今插入數據參數 -
data: {'source':'hello'}
data: "source=hello"
data: {translations:{'source':'hello'}}
我知道如何做到這一點與軌道形式,但我仍然想與它的jQuery。我懶惰翻譯我的JavaScript函數爲紅寶石,這耗費我很多時間。無論如何,它的工作原理,謝謝:) – Ariel 2011-04-15 10:01:20