如何将超过2个参数传递给partial?

时间:2013-01-21 21:57:14

标签: ruby-on-rails ruby-on-rails-3

我想传递两个参数,例如topicicon_photo 我怎么能这样做?

  

未定义的方法`icon_photo?'

我在下面的代码中遇到了这个错误。

查看

<div class="Topic">
  <% @community.topics.each do |topic| %>
    <%= render 'topics/topic', :topic => topic, :icon_photo => topic.user.profile.avatar %>
  <% end %>
</div>

1 个答案:

答案 0 :(得分:7)

您可以传递本地哈希:

<div class="Topic">
  <% @community.topics.each do |topic| %>
    <%= render 'topics/topic', locals: {topic: topic, icon_photo: topic.user.profile.avatar, etc: 'blabla' } %>
  <% end %>
</div>

在此处查看一些文档http://www.tutorialspoint.com/ruby-on-rails/rails-render.htm

稍微改进可以是mabe,您可以像这样呈现您的收藏:

<div class="Topic">
  <%= render partial: 'topics/topic', collection: @community.topics %>
</div>


# in your partial topics/_topic.html.erb:
<% icon_photo = topic.user.profile.avatar %>