简体   繁体   中英

How can I pass more than 2 arguments to partial?

I want to pass 2 arguments such as topic and icon_photo .
How can I do that?

undefined method `icon_photo?'

I got this error with the code below.

view

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

You can pass a locals hash:

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

See some documentation here: http://www.tutorialspoint.com/ruby-on-rails/rails-render.htm

A little improvement can be mabe, you can render your collection like this:

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


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM