2015-11-03 105 views
2

所以我在我的rails應用程序中有一個側欄。基本上,我有一個充滿圖像的網格。當有人點擊網格中的圖像時,我基本上希望顯示視圖顯示在邊欄中。我對如何做到這一點毫無頭緒。在部分渲染顯示視圖 - Ruby on Rails

我試着複製和粘貼顯示視圖的html和erb到我的_sidebar.html.erb部分。但我得到可變錯誤。我的部分位於我的佈局文件夾中。但如果需要,我可以將其移動。

這是應用程序的圖像,以更好地瞭解我在說什麼。

This is what my app looks like. The gray part is the sidebar partial.

+0

我建議閱讀和學習Rails的AJAX和JS視圖約定。有很多方法可以做你想做的事情,所以你需要學習如何實現它。回來,你有一個代碼特定的問題或問題,我們會幫助。閱讀這些:http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html和https://coderwall.com/p/kqb3xq/rails-4-how-to-partials-ajax-dead-easy –

回答

1

比方說,你的模式被稱爲 「圖像」。

在你的 「圖像/ 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> 

在你的「圖像/ _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 %>"); 
+0

哇,謝謝你這樣詳細的答案!我有幾個問題。 首先,在我的posts/show.html.erb中。我爲什麼要渲染@post?當我嘗試添加'<%= render @post%>'時,我得到一個錯誤,指出「每個'未定義的方法對於nil:NilClass」。在我的節目控制器中,我添加了@post = Post.find(params [:id])。 其次,爲我的網格link_to。我可以只做'<%= link_to'顯示',發佈,遠程:真正%>'或者不正確? 非常感謝你的幫助。 如果解釋太多,我明白。生病繼續讀哈哈。 –

+0

通過博客,你的意思是我稱爲形象吧? 如果你想呈現多個帖子,你不應該在你的帖子#show中做,而是在你的帖子#index中做實例。 部分_post.html.erb不應該包含循環,它應該只顯示帖子本身。如果你想渲染幾個帖子,只需使用: render \ @posts。 \ @posts是帖子的'數組',它會爲數組中的每個帖子調用_post.html.erb。 <%= link_to'Show',post,remote:true%>'可以,如果你有posts/show.js.erb。 – Statyx

2

這裏是如何做到這一點:


1)包括在你佈局sidebarpartial

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

這將調用_sidebar.html.erb部分在/app/views/shared


2)填寫了 「默認」 側邊欄部分:

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

這裏要注意的重要一點是,諧音應該永遠有內部@instance變量。

局部模板有locals,您可以用下面的命令傳遞:

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

局部模板是爲了在你的web應用程序的任何一部分運行,而且由於@instance變量存在於一個單一的對象的「實例」(IE @post/users中將不可用),您可以確保通過將局部變量傳遞給它們來始終填充部分。

這就是爲什麼你複製你的post#show碼到您的sidebar部分時收到錯誤 - 實例本作的show行動將不會出現在應用程序的其它部分的變量。


3)使用JS拉圖像對象

當有人點擊網格中的圖像上,我基本上要顯示在側邊欄顯示視圖。

您需要更具體的 - 你真的「秀」的動作出現,還是希望它的功能部件?

無論哪種方式,你會使用JS &阿賈克斯提取數據是最好的:

#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動作(無佈局),並將它添加到您的側邊欄部分。這就是你要求的 - 如果需要,我可以改變它。

+0

HI Rich!到目前爲止,我已經能夠使邊欄正確呈現帖子視圖。通過使用'<%= render「佈局/側邊欄」,:post => @post%>'。因此,我的部分erb例如像'<%= post.title%>',並且工作正常。我試着添加你給我的JavaScript以及控制器代碼,但它似乎沒有工作。我不確定在哪裏放置控制器代碼,所以我把它放在show方法下,並刪除了「佈局」,因爲它給我一個錯誤。有什麼建議麼?這裏是github鏈接https://github.com/mattietea/musicapp。非常感謝! –

+0

你不需要指定「本地」。 <%=渲染「共享/側欄」,變量:「值」%>作品 – Statyx