Ruby on rails:是否可以嵌套部分调用/渲染,如下所示:

时间:2015-07-27 18:59:31

标签: ruby-on-rails ruby view nested partials

假设"书籍/书"部分下面是从多个位置调用的(例如:作者#show view,categogories #show view,categories #index view),我只想从作者#show view中调用部分投票。也就是说,我希望从书籍部分(@upvoted_books)返回的书籍集合每个呈现"投票/投票" partail,以便作者能够从他们的作者#show view投票。以下代码或其变体是否明智?

    <%= render partial: "books/book", collection: @upvoted_books  do |upvoted| %>
  <%= render partial: "votes/vote", locals: {category: book.category, book: upvoted } %>

<% end %>

p.s:我在这里尝试了这个特殊的代码并且它在我的项目中不起作用,但我只是想知道类似于它的东西是否可以工作,但它也不会引发任何明显的错误。也许我只是把语法混淆了一下。我也知道其他一些方法来解决同样的问题但我正在寻找更优雅的东西。感谢您对您的创意反应。

更新

感谢@Deep,感谢您提供非常有见地的解决方案。我之前为解决这个问题所做的是从#34; books / book&#34;部分:

<% if defined?(@upvoted_books) && @upvoted_books.include?(book) %>
  <%= render partial: "votes/vote", locals: {category: book.category, book: upvoted } %>
<% end %>

虽然有效,但我不确定它是否有一些重要的代码嗅觉,或者它是否具有合成性的尴尬。你觉得怎么样?

最终更新:

@Deep这些是你要求的观点:这是我的类别#show view:

`<h1>Topics#show</h1>

<p><%= @category.title %></p>

<%= render partial: "books/form", locals: {topic: @category, book: Book.new} %>

<%= link_to "edit category", edit_author_category_path(@category.author, @category) %>

<p> Books you have Authored </p>

<%= render partial: "books/book", collection: @category.books %>`

 And this is my books/_book.html.erb partial now    

    `<% if defined?(upvoted_book) && current_author.books.include?(upvoted_book) %>

  <h5>Created by <%= upvoted_book.category.author.name || upvoted_book.category.author.email %> on <%= upvoted_book.created_at %></h> 
  <br>

  <%= link_to upvoted_book.title, category_book_path(upvoted_book.category, upvoted_book) %>

  <% if policy(Vote.new).create? %>

    <%= render partial: "votes/vote", locals: {category: upvoted_book.category, book: upvoted_book} %>
  <br>

  <% end %>

<% else %>

  <h5>Created by <%= book.category.author.name || book.category.author.email %> on <%= book.created_at %></h> 
  <br>

  <%= link_to book.title, category_book_path(book.category, book) %>

  <%= link_to "Remove book", category_book_path(book.category, book), method: :delete, data: { confirm: "Are you sure you want to delete this book?" } %>

<% end %>`

如您所见,我在局部变量中引用局部变量book和upvoted_book。这就是我从作者#show view:`

中调用部分的方式
<%= render partial: "categories/form", locals: {category: Category.new, author: @author} %>

<p> Categories and Books you have Writen: </p>

<%= render partial: "categories/category", collection: @categories %>

<p> Books from all authors that you have Upvoted: </p>

<%= render partial: "books/book", collection: @upvoted_books, as: :upvoted_book %>
<br>  `

Thanks for you wonderful insight again. 

1 个答案:

答案 0 :(得分:1)

由于您需要在每本书上展示部分投票,并且只在一个视图中展示,所以您可以做的是:

<%= render partial: "books/book", collection: @upvoted_books, locals: { vote: true } %>

现在在您的部分中写下:

if vote
   <%= render partial: "votes/vote", locals: {category: book.category, book: upvoted } %>
end

确保从其他地方渲染book部分传递locals: { vote: false },否则会抛出错误。或者你可以做的部分是:

vote ||= false

或者您可以使用if vote代替使用if defined?(vote),因此从其他网页传递它不会有任何负担。

此外,我怀疑你有一些错误的当地人,不确定可能是如此正确他们。但是使用此功能,您可以在单个视图上调用vote部分。

希望这有帮助。