不幸的是,turbolinks 和 pjax 本身都不可能(拥有多个所谓的 pjax 容器)。
Turbolinks 总是替换整个 body 元素的内容。Pjax 总是用pjax-container
数据属性替换元素的内容。
当然有办法做到这一点。抛弃客户端 JavaScript 框架,您可以执行 ajax 请求并返回js
响应。
从我的脑海中,您可以看到如下所示的视图:
index.html.erb
<%= link_to 'Show', show_path, remote: true %>
控制器
def show
# Do the work, fetch data from database etc.
# Keeping both formats ensures that when you hit the url directly
# the whole page gets rendered as usual (show.html.erb),
# when the request is an ajax request using the view snipper above just
# the javascript for replacing the content of a single panel is rendered (show.js.erb)
#
# Rails is smart enough so it should not be required to include the respond_to block
# in the controller, Rails automagically chooses an appropriate response format
# based on the request, you only need to have the views in place,
# but I keep it here so you get a picture.
respond_to do |format|
format.html
format.js
end
显示.js.erb
$('.right-panel').html('<%= j render partial: "something" %>')