[英]rails - load another page and replace the div
經過一番掙扎,我設法使 ajax 工作:
你好.js.erb:
$("#show_hosts").html("some text");
testQueuesController.rb:
class testQueuesController < ApplicationController
def index
@test_queues = testQueue.all
respond_to do |format|
format.html
format.json { render json: @test_queues }
end
end
end
視圖中的相關部分:
<%= link_to "hello", { :controller => "kashmir_queues", :action => "hello" }, :remote => true %>
現在,這是有效的,但我真正想做的是加載“一些文本”,但是要加載另一個我命名為 show_hosts.html.erb 的視圖,我該怎么做?
如果我正確理解您的問題,您希望在 ID 為show_hosts
的 HTML 標記中呈現名為show_hosts.html.erb
的視圖。
為了能夠做到這一點,您需要為每個主機創建一個部分 - 類似於_host.html.erb
並在hello.js.erb
編寫以下hello.js.erb
:
$('#show_hosts').html(<%= render partial: 'host', collection: @hosts %>)
其中@hosts
是我假設您用於在show_hosts.html.erb
顯示的主機集合的show_hosts.html.erb
,但我必須承認我有點困惑,因為您在控制器中使用@test_queues
。
對於它的價值(如果我沒記錯的話),您還需要將format.js
添加到您的控制器以呈現hello.js.erb
模板。
在您的控制器中添加名為 hello 的新操作
def hello
queue = KashmirQueue.find(params[:id])
@test_queues = testQueue.where(:host => queue.host)
end
將你的 show.html.erb 移動到像 _show.html.erb 這樣的部分
然后在 hello.js.erb 中寫入
$("#show_hosts").html('#{escape_javascript(render "show.html.erb")}');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.