我想传递 2 个参数,例如 topic
和 icon_photo
。
我该怎么做?
未定义的方法"icon_photo?
我在下面的代码中遇到了此错误。
视图
<div class="Topic">
<% @community.topics.each do |topic| %>
<%= render 'topics/topic', :topic => topic, :icon_photo => topic.user.profile.avatar %>
<% end %>
</div>
您可以传递局部变量哈希:
<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 %>