有兩種方法可以做到這一點。
首先,如果你想加載JS 上頁,你需要使用什麼@anchalee
建議,並與某種在CSS「隱藏」類的預加載內容:
#app/assets/javascripts/application.js
$(document).on("click", "a.hidden", function(e){
e.preventDefault();
el = $(this).attr("href");
$(el).toggle();
});
#app/views/controller/your_view.html.erb
<%= link_to "User", "#user", class: "hidden" %>
<div id="user" class="hidden">
Hello
</div>
這將需要由鏈路的href
屬性指定的id
任何的div,然後讓這取決於其當前狀態div
將hide
或show
做到這一點。
-
其次,你有關閉頁方法(使用ajax
)。
這用於從服務器獲取數據,並且將會載入以服務器爲中心的數據,例如您現在正在嘗試執行的操作。
這樣做其實很簡單:
#app/controllers/your_controller.rb
class YourController < ApplicationController
layout: Proc.new{|c| !c.request.xhr? }
end
這將返回你需要沒有任何layout
數據:
#app/assets/javascripts/application.js
$(document).on("click", "#show", function(e){
e.preventDefault();
$.get($(this).attr("href"), function(data){
$("#show").html(data);
});
});
#app/views/your_controller/show.html.erb
<%= link_to "Show", [[url for page]], id: "show" %>
好吧,如果你使用的資產,你不應該是混合的JavaScript和軌道。怎麼樣ajax? – lcguida