sending specific data into a collection partial

Posted by mikeglaz on Stack Overflow See other posts from Stack Overflow or by mikeglaz
Published on 2012-09-22T03:35:50Z Indexed on 2012/09/22 3:37 UTC
Read the original article Hit count: 180

I have a User class with a has_many :messages and a Message class which belongs_to :user. In the Message controller's show action has the corresponding view:

<% if @messages.any? %>
  <ol class="microposts">
    <%= render partial: 'shared/message', collection: @messages %>
  </ol>
<% end %>

And the shared/_message.html.erb template looks like this:

<li id="<%= message.id %>">
  <span class="content"><%= message.body %></span>
  <% user_id = message.from %>
  <% user = User.find(user_id) %>
  From: <%= user.name %>
</li>

I feel like the following two lines should be done in the Messages controller from what I read in tutorials on Rails:

<% user_id = message.from %>
<% user = User.find(user_id) %>

But how would I pass each message's corresponding from value (which stores user.id) into the partial?

thanks, mike

© Stack Overflow or respective owner

Related posts about ruby-on-rails-3

Related posts about railstutorial.org