2013-01-08 64 views
1

我想基於html中的數據更改Workorder.wostatus_id。Rails ajax更新

在我的索引HTML,我已經存儲了這樣的wostatus.id:

<span id="woid">4</span> 

我想更新workorder.wostatus_id = 4

這是workorders.js.coffee - 但它不起作用:

$.ajax 
    type: 'POST' 
    url: 'http://localhost:5000/workorders' 
    data: 
     workorder: 
     wostatus_id: $("#woid").val() 

也許我沒有找到正確的工單記錄?

即使這樣做沒有更新workorder.wostatus_id

 $.ajax 
     type: 'POST' 
     url: "http://localhost:5000/workorders" 
     data: 
     workorder: 
      wostatus_id: '3' 

這並沒有工作,要麼:

 $.ajax 
     type: 'POST' 
     url: "http://localhost:5000/workorder/17" 
     data: 
     wostatus_id: '7' 

我失去了一些大的時間。

ajax POST是否在工單控制器中執行此代碼?

# PUT /workorders/1 
# PUT /workorders/1.json 
def update 
    @workorder = Workorder.find(params[:id]) 

    respond_to do |format| 
    if @workorder.update_attributes(params[:workorder]) 
    format.html { redirect_to @workorder, notice: 'Workorder was successfully updated.' } 
    format.json { head :ok } 
    else 
    format.html { render action: "edit" } 
    format.json { render json: @workorder.errors, status: :unprocessable_entity } 
    end 
end 

UPDATE:

我已將此添加到工作單控制器:

def changestatus 
    @workorder = Workorder.find(params[:id]) 
    @workorder.update_attribute :wostatus_id, '4' 
    render nothing: true 
end 

我已將此添加到路線:

resources :workorders do 
    member { put :changestatus } 
    end 

這是目前在js.coffee :

$.ajax 
    type: 'PUT' 
    url: "http://localhost:5000/workorders/11/changestatus" 
    data: 
     wostatus_id: 4 

(我硬編碼的東西,直到我得到了下一步的工作。)

SO - 這工作,工作單11變爲wostatus_id改爲4

但是,現在我遇到了麻煩來自html的正確信息。 該html包含我需要的兩個數據字段 - 一個是工作單,另一個是wostatus_id。

這裏是更新URL的HTML:

<div class="false" data-change-url="http://localhost:5000/workorders/16/changestatus"> 

我認爲這將獲取網址 - 但是,它不工作:

$(this).data('change-url') 
+0

'更新'行動代碼,請? –

+0

我更新了問題。 – Reddirt

回答

0

發現我並不需要任何新的控制器代碼 - 我可以只使用更新。 這是爲jquery-ui排序。

receive: (event, ui) -> 
    str_id = $(ui.item).attr('id') 
    woid = str_id.split('_')[1] 
    $.update "/workorders/" + woid, 
     workorder: 
     wostatus_id: $(this).data('wostatus-id') 

感謝您的幫助 - 您讓我朝着正確的方向前進。

1

如果我理解正確的話,那麼我當你的控制器需要一個陣列並且你正在使用不同的參數名稱(客戶端上的wostatus_id,服務器上的workorder))時,請考慮發送單個值。

也許你想要的是這樣的:

$.ajax 
    type: 'POST' 
    url: $('#sort2').data('update-url') 
    data: 
    workorder: $('#sort2 span').map((i, el) -> 
     el.text() 
    ) // Change the selector to the elements that holds the ID 
+0

我更新了我的問題以取消排序路線。 – Reddirt

+0

你知道我們不知道在控制器中發生了什麼,也可能不知道它背後的模型,請發佈這些相關的代碼? –

+0

我添加了控制器代碼 - 我想。我不明白哪個控制器def POST執行。 – Reddirt