[英]best_in_place using :display_as - how do I update the value of the <input> as well as the <span>
[英]When I make a change with best_in_place, I want another text value on the page updated
在我看來,我應該能夠使用javascript進行JSON解析,以返回任何respond_with_bip(@buildingTour)
返回的值,因為那是我需要的更新對象(我需要訪問@buildingTour.monthly
的更新值)。 但是,當我嘗試解析它時,我沒有運氣。 我已經讀過一些有關response_with_bip破壞JSONParse的內容,所以也許就是這樣。
如果有人可以幫助我解析@buildingTour以獲取每月屬性(即,向我展示JavaScript),將不勝感激。
@MikeKijewski-需要澄清的是,我的視圖是顯示視圖,即我們僅處理一行。 我知道您所指的SO文章是處理具有多行的表,這有點棘手。 我不明白的是,為什么控制器中的“ respond_with_bip”尚未返回我需要的東西。 這是我的控制器:
def update
@buildingTour = BuildingTour.find(params[:id])
respond_to do |format|
if @buildingTour.update_attributes(params[:building_tour])
format.json { respond_with_bip(@buildingTour) }
else
format.json { respond_with_bip(@buildingTour) }
end
end
end
那么,為什么不能使用“ respond_with_bip”返回的數據來更新頁面上的字段?
現在,我只是使用location.reload();
每當有問題的兩個字段(平方英尺和租金)用best_in_place更新時,該字段就會顯示第三個字段的適當值(每月)。
我在我的Rails應用程序中使用best_in_place
(用於就地編輯sqft和rate的值),它工作得很好,但有一個例外:
我的模型中有一個計算得出的屬性,稱為monthly
。 當我使用best_in_place
更改值時,我需要在頁面上重新計算並更新該monthly
值。
這是我的模型中monthly
的代碼:
def monthly
if sqft && rate
monthly = (sqft * rate) / 12
end
end
這是我看來的代碼段(這是我要更新的文本-它位於ID為“ rental-details”的范圍內):
<strong><%= number_to_currency(@buildingTour.monthly, :precision => 0) %></strong>
這是我在進行best_in_place
更改時可以識別的javascript,然后在理論上更新上述值:
$('.best_in_place').change(function() {
$.ajax({
success: function() {
$("#rental-details strong").text('<%= number_to_currency(@buildingTour.monthly, :precision => 0) %>');
}
});
});
因此代碼全部“正常”運行而沒有錯誤。 。 。 除了它正在拉動每月的舊值。 我通過從JavaScript中刪除number_to_currency
進行了測試,並且很確定地,當我更改其他值之一時,它會從頁面上的值中刪除貨幣格式(因此我知道JavaScript正在被觸發並正在更新文本)。
我以為JavaScript可能觸發得太快了(並且尚未用sqft和rate的新值更新數據庫),但是我嘗試多次更改sqft和rate的值,而月度值從未更新。
因此,無論如何,JavaScript的<%= number_to_currency(@buildingTour.monthly, :precision => 0) %>
部分僅在頁面加載之類的時候被調用一次嗎? 如果是這樣,您如何強制它monthly
一次查詢?
需要指出的一點是…… monthly
不是building_tours
表中的一列,我只是即時進行計算。
感謝您對此問題的任何見解。 我已經用谷歌搜索和堆棧溢出了,這些都使我感到我當前的代碼應該可以工作。
我最終創建了一個javascript函數,該函數監視要更新的特定類(.best_in_place或類似的元素)的元素,然后從控制器操作(在您的情況下為BuildingTour?)中獲取json響應,並替換為表格行包含更新的.monthly值的新表行。 看看這篇SO文章:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.