1

我有一个 Ruby on Rails 应用程序,它最基本的级别由两个面板组成,左侧面板和右侧面板。我想要的是将其用作布局,并在控制器中进行渲染render <view_name>, <right or left panel>,以便与 turbolinks 结合使用,仅动态创建左侧或右侧面板。

我搜索了互联网,但没有找到像我这样的例子。我确定这是可行的,但如何?
编辑:它应该能够在 chrome for Android 中运行。一种解决方案是在第一个请求上呈现所有视图,当我单击链接时,面板会被隐藏并显示,但我认为这不行。有什么意见吗? 在此处输入图像描述

4

3 回答 3

1

不幸的是,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" %>')
于 2013-10-22T07:55:38.103 回答
0

经过一番研究,我得出以下结论:

除非你设置一些限制,否则我试图做的事情是不可能的。

两个面板中只有一个可以呈现存储在浏览历史中的“动作”。如果两个面板都呈现存储在浏览历史记录中的操作,则导航很可能无法按预期工作。

考虑到这一点,我意识到我真正需要的是在左侧面板中呈现导航感知操作,并在右侧面板中呈现其他内容(我称它们为实用程序)。例如,我在这样的面板中呈现的一件事是聊天面板。这就是 facebook 如何使用它的聊天。

所以基本上我可以使用标准 turbolinks 来处理存储浏览历史记录的请求以及类似“Utils”面板的 pjax 之类的请求。唯一的问题是 Turbolinks 会完全呈现整个页面,即使您发送一小段 html 作为响应也是如此。

为了解决这个问题,我最终使用了 wiselinks,它是 turbolinks 和 pjax 的完美结合。此外,与 pjax 不同,wiselinks 甚至可以处理表单。

于 2013-12-05T21:03:08.603 回答
0

我不知道这是否是最佳实践,但我需要类似的东西,这就是我所做的:

我有这个布局

1st panel | 2nd panel  | 3rd panel
Site Menu | Posts List | Article Show

要始终呈现帖子列表,我需要添加到我的应用程序控制器:

class ApplicationController < ActionController::Base
  before_action :set_posts

  def set_posts
    @posts = Post.all
  end
end

然后在我的布局中,这是我所做的:

%body
    #site-wrapper
      #site-canvas
        #reading-list
          #reading-nav-block 
          = render 'posts/list'

        #article-show
          #article-nav-block
          #article-content-block
            = yield
        #site-menu
          #logo-block
          .menu

基本上我将posts#index提取到posts#_list部分并设置变量,在所有布局中呈现post/_list。结果如下:

在此处输入图像描述

于 2015-07-15T10:38:03.563 回答