0
我chap_three_controller.rb文件:爲控制器創建函數的route.rb中添加了哪些路由?
class ChapThreeController < ApplicationController
def create
@marker = Marker.new(params[:m])
if @marker.save
res={:success=>true,:content=>"<div><strong>found </strong>#{marker.found}
</div><div><strong>left </strong>#{marker.left}</div>"}
else
res={:success=>false,:content=>"Could not save the marker"}
end
render :text=>res.to_json
end
end
我的routes.rb文件
match '/map3', :to => 'chap_three#map'
match '/map3/create', :to => 'chap_three#:action'
我這樣做是正確的匹配控制器我的路線創造功能?因爲它不工作..
這是我的JavaScript代碼片段:
request.open('GET', 'create' + getVars, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
//the request is complete
var success=false;
var content='Error contacting web service';
try {
//parse the result to JSON (simply by eval-ing it)
res=eval("(" + request.responseText + ")");
content=res.content;
success=res.success;
}catch (e){
success=false;
}
我不斷收到的錯誤是「錯誤聯繫網絡服務」。這意味着,我的request.responseText不工作,並在我的控制器創建方法沒有做任何事情....任何幫助將是巨大的
嘗試通過@marker = Marker.new(params [:m])將參數[:m]存儲到數據庫...但它不能正常工作..這是一個質量分配問題?無法弄清楚 –
沒關係,你添加到URL的任何查詢參數都可以在'params'中找到。所以如果你的網址是'/ map3/create?m [left] = some_value&m [found] = true',那麼'params [:m]'會有'left'和'found'的值。 –
這可能是一個大規模分配問題。確保你有'attr_accessible'你正在發送的值。您還可以檢查「@ marker.save」是否返回「false」,如果是,請檢查「@ marker.errors」以瞭解它失敗的原因。你也可以將它切換到'@ marker.save!',它會拋出一個異常,並可能在調試過程中更明顯。 –