【问题标题】:Rails 4 - rendering a collection with a partial name different to the variableRails 4 - 使用与变量不同的部分名称呈现集合
【发布时间】:2014-04-15 05:38:06
【问题描述】:

我的项目中有以下部分posts/post_preview

.post
  %h2= link_to(post.title, post)
  .post-body= simple_format(post.body)
  %ul
    - post.tags.each do |tag|
      %li= tag

我想为集合名称@posts 渲染此部分,如下所示:

render partial: 'post_preview', collection: @posts

但是,render 默认情况下将集合的每个成员作为以局部变量命名的局部变量传递。有什么办法可以改变这个,所以@posts 的每个成员都被传递到一个名为post 的变量中的部分?

【问题讨论】:

  • 好主意。为什么需要这种功能?

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

http://guides.rubyonrails.org/layouts_and_rendering.html 检查此以更改名称 假设您有产品并想用作商品,然后就照做

<%= render partial: "product", collection: @products, as: :item %>

我建议你也检查一下。 http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    相关资源
    最近更新 更多