1

我想传递 2 个参数,例如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>
4

1 回答 1

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

可以做一点改进,你可以像这样渲染你的收藏:

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


# in your partial topics/_topic.html.erb:
<% icon_photo = topic.user.profile.avatar %>
于 2013-01-21T21:58:53.520 回答