[英]Ruby on Rails Partial won't refresh with AJAX but says it is rendering the partial in the console
我正在嘗試獲取一張表以刷新我的Rails 5應用程序。 它被托管在Linux CentOS 7系統上,並且可以通過Windows 7上的Firefox瀏覽器進行訪問。我嘗試了很多研究,並嘗試了Rails-AJAX gem無效(該寶石由於某些原因也破壞了我的鏈接,因此我將其禁用了現在)。 在我的控制台中,它聲稱在對數據庫進行更改后將呈現部分視圖,但是直到我在瀏覽器中手動刷新頁面后,該更改才會反映出來。
我要刷新的部分數據是整個表,其中填充了數據庫中的數據,並且該表具有開/關指示燈的圖像,具體取決於數據庫中的狀態是“開”還是“關”。 如果實際上正在刷新表,則此圖像是我尋找的圖像。 不幸的是,此圖像僅在我手動刷新時才更改。 注意:Svc / svc是我正在使用的數據庫中模型/表的名稱。
這是單擊“開始”按鈕時收到的控制台消息:
Started GET "/boundbooks/1" for *IP ADDRESS* at 2018-06-29 17:51:10 -0400
Cannot render console from *IP ADDRESS*! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by BoundbooksController#startServer as JS
Parameters: {"id"=>"1"}
Svc Load (0.8ms) SELECT "svc".* FROM "svc" WHERE "svc"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/boundbooks_controller.rb:26
(0.2ms) begin transaction
↳ app/controllers/boundbooks_controller.rb:30
Svc Update (4.2ms) UPDATE "svc" SET "STATUS" = ? WHERE "svc"."id" = ? [["STATUS", "on"], ["id", 1]]
↳ app/controllers/boundbooks_controller.rb:30
(23.8ms) commit transaction
↳ app/controllers/boundbooks_controller.rb:30
Svc Load (0.5ms) SELECT "svc".* FROM "svc"
↳ app/views/boundbooks/_boundbooks_table.html.erb:8
Rendered boundbooks/_boundbooks_table.html.erb (17.8ms)
Completed 200 OK in 83ms (Views: 36.3ms | ActiveRecord: 29.5ms)
來自views / boundbooks / index.html.erb的代碼:
<div id="bb-table">
<%= render partial: 'boundbooks_table', locals: { boundbooks: @boundbooks } %>
</div>
<br><br>
<% @boundbooks.each do |svc| %>
<%= link_to 'Refresh', boundbookUpdate_path(:bb_id => svc.id), id: 'refresh-button', class: 'btn', remote: true %>
<% end %>
是的,我知道刷新按鈕已打印多次,但是我不知道如何將一個按鈕鏈接到數據庫。
表部分的HTML:
<table id="boundbooktable" border="1">
<thead><th>Status</th><th>Name</th><th>IP Address</th><th>Port</th><th>Action</th></thead>
<tbody>
<%# btnCounter gives each button a different ID by incrementing at the end of the loop %>
<% btnCounter = 1 %>
<%# statusOn = true %>
<%# creates a for-loop that utilizes the svc database table %>
<% @boundbooks.each do |svc| %>
<% status = svc.STATUS %>
<tr>
<td class="status-cell">
<% if status == "on" %>
<%= image_tag ("statusOn.png") %>
<% else %>
<%= image_tag ("statusOff.png") %>
<% end %>
</td>
<td><%= svc.NAME %></td>
<td><%= svc.IP %></td>
<td><%= svc.PORT %></td>
<td>
<% if status == "off" %>
<%= link_to "Start", boundbookStart_path(svc.id), id: 'start-button' + btnCounter.to_s, class: 'btn-start',
data: { confirm: "Start " + svc.NAME + "?" }, remote: true %>
<% else %>
<%= link_to "Start", boundbookStart_path(svc.id), id: 'start-button' + btnCounter.to_s, class: 'btn-start',
data: { confirm: svc.NAME + " is already on." }, remote: true %>
<% end %>
<% if status == "on" %>
<%= link_to "Stop", boundbookStop_path(svc.id), id: 'stop-button' + btnCounter.to_s, class: 'btn-stop', remote: true,
data: { confirm: "ALERT: Stop " + svc.NAME + "?" }, onclick: 'myFunction()' %>
<% else %>
<%= link_to "Stop", boundbookStop_path(svc.id), id: 'stop-button-already-off' +btnCounter.to_s, class: 'btn-stop',
remote: true, data: { confirm: svc.NAME + " is already off." } %>
<% end %>
<%= link_to "Log", boundbooks_path, id: 'log-button' + btnCounter.to_s, class: 'btn btn-log', remote: true %>
</td>
</tr>
<% btnCounter += 1 %>
<% end %> <%# end for-loop going through database %>
</tbody>
</table>
綁定書控制器更新和startServer方法的代碼:
def update
@boundbooks = Svc.all
@selected = Svc.where(:boundbook_id => params[:bb_id])
respond_to do |format|
format.js
end
end
def startServer
server = Svc.find(params[:id])
if server.STATUS == "off"
@boundbooks = Svc.all
server.update_attribute(:STATUS, "on")
refresh_dom_with_partial('div#bb-table', 'boundbooks_table')
render partial: 'boundbooks_table', locals: { boundbooks: @boundbooks }
else
puts ">>>> >>>> >>>> Server already on"
end
end
在boundbooks視圖文件夾中的update.js.erb中用於AJAX的JavaScript代碼:
$.ajax({ url: "boundbooks_controller/update",
success: function() {
$("#boundbookstable").html("<%= escape_javascript(render partial: 'boundbooks_table', locals: { boundbooks: @selected } ) %>");
}});
這些是route.rb中的相關路線:
get '/welcome' => 'welcome#index'
get '/boundbooks/:id/', controller: 'boundbooks', action: 'startServer', as: 'boundbookStart'
get '/boundbooks/:id/edit', controller: 'boundbooks', action: 'stopServer', as: 'boundbookStop'
get '/boundbooks/:bb_id/update' => 'boundbooks#update', as: 'boundbookUpdate'
get '/boundbooks_controller/update' => 'boundbooks#update'
我將如何獲取它以實際正確刷新表?
您的錯誤似乎很簡單
您正在嘗試更改ID為'boundbookstable'的div的html
$("#boundbookstable").html(...
但在您的html中,div的ID為“ boundbooktable”
<table id="boundbooktable" border="1">
只要確保它們在兩個文件上完全相同,就可以更改html或js,但是請確保兩個文件上的它們都相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.