【问题标题】:Render show view in a partial - Ruby on Rails部分渲染显示视图 - Ruby on Rails
【发布时间】:2015-11-03 18:03:28
【问题描述】:

所以我的 rails 应用程序中有一个侧边栏。基本上,我有一个充满图像的网格。当有人单击网格中的图像时,我基本上希望显示视图显示在侧边栏中。我对如何做到这一点一无所知。

我尝试将显示视图的 html 和 erb 复制并粘贴到我的 _sidebar.html.erb 部分中。但我得到可变错误。我的部分位于我的布局文件夹中。但如果需要,我可以移动它。

这是应用程序的图像,以便更好地了解我在说什么。

【问题讨论】:

标签: ruby-on-rails partials


【解决方案1】:

这是怎么做的:


1) 在您的布局中包含sidebar partial

#app/views/layouts/application.html.erb
<%= render "shared/sidebar" %>

这会调用/app/views/shared 中的_sidebar.html.erb 部分:


2) 填充“默认”侧边栏部分:

#app/views/shared/_sidebar.html.erb
...

这里要注意的重要一点是,partials 应该永远在内部包含 @instance 变量。

Partials 有locals,您可以使用以下命令传递:

<%= render "shared/sidebar", locals: { variable: "value" } %>

Partials 旨在在您的 Web 应用程序的 any 部分上运行,并且由于 @instance 变量存在于对象的单个“实例”(IE @ 987654333@ 在/users 中不可用),您可以通过将局部变量传递给它们来确保始终填充部分。

这就是您在将post#show 代码复制到sidebar 部分时收到错误的原因——show 操作的实例变量不会出现在应用的其他部分。


3) 使用JS拉取图片对象

当有人点击网格中的图像时,我基本上希望显示视图显示在侧边栏中。

您需要更具体 - 您真的想要“显示”动作出现,还是想要它的部分功能?

无论哪种方式,你最好使用 JS 和 ajax 来拉取数据:

#app/controllers/images_controller.rb
class ImagesController < ApplicationController
   layout Proc.new { |controller| !controller.request.xhr? }
end

#app/assets/javascripts/application.js
$(document).on("click", "img", function(e) {
   e.preventDefault();
   $.get($(this).attr("href"), function(data){
      $(".sidebar .image").html(data);
   });
}); 

这将采用呈现的images#show 动作(无布局),并将其附加到您的 sidebar 部分。这是您要求的 - 如果需要,我可以更改它。

【讨论】:

  • 嗨,丰富!到目前为止,我已经能够让侧边栏正确呈现帖子视图。通过使用 ' @post %>' 。所以我的部分有像''这样的erb,并且可以正常工作。我尝试添加您给我的 javascript 以及控制器代码,但它似乎不起作用。我不确定将控制器代码放在哪里,所以我把它放在 show 方法下并删除了“布局”,因为它给了我一个错误。有什么建议么?这是github链接github.com/mattietea/musicapp。非常感谢!
  • 您不需要指定“本地”。 works
【解决方案2】:

假设您的模型称为“图像”。

在您的“images/show.html.erb”中:

# This is a handy shortcut that will call the 'images/_image' partial. 
# Those two lines are exactly the same. 
<%= render @image %>
<%= render "images/image", image: @image %>

在您的“_sidebar.html.erb”中:

<div id="sidebar-img"></div>

在你的“images/_image.html.erb”中:

 # Code displaying your image, don't use any global variable.
 # 'image' is the name of the local variable

在您的网格中:

 # remote: true allows you to do an AJAX call
 <%= link_to image, remote: true do %> 
   # Something like that, I don't know your code
   <%= image_tag image.url %>
 <% end %> 

创建“images/show.js.erb”,当点击“remote :true”链接时,它会调用这个文件。

 # set @image to Image.find(params[:id]) in your controller
 $("#sidebar-img").html("<%= render @image %>");

【讨论】:

  • 哇,谢谢你这么详细的回答!我有几个问题。首先,在我的帖子/show.html.erb 中。我为什么要渲染@post?这是我的 for 循环在我的网格中创建帖子的部分。当我尝试添加 '' 时,我收到一个错误,上面写着 "undefined method `each' for nil:NilClass" 。在我的节目控制器中,我添加了'@post = Post.find(params[:id])。其次,对于我的网格 link_to。我可以只做 '' 还是不行?非常感谢你的帮助。如果解释太多,我理解。我会继续阅读哈哈。
  • 通过帖子,你的意思是我所谓的图像对吗?如果你想渲染多个帖子,你不应该在你的 posts#show 中进行,而是在你的 posts#index 中进行。部分 _post.html.erb 不应包含循环,它应该只呈现帖子本身。如果要渲染多个帖子,只需使用:render \@posts。 \@posts 是一个帖子的“数组”,它将为数组中的每个帖子调用 _post.html.erb。 ' 如果你有一个 posts/show.js.erb 就可以了。
猜你喜欢
  • 2013-06-24
  • 2017-05-07
  • 1970-01-01
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多