1

I'm having trouble with attempting to render a partial within a loop. Essentially, I have an app where events are created, people join the event, and for each event, attendees have a list of items they will bring which they can select from their own inventory of items.

In my show.html.erb for Events:

           <% @event.attendees.each do |user| %>
            <div class="col-md-4">
              <h4><%= user.first_name %> <%= user.last_name %></h4>

                <% if user.event_items.any?  %>

                <div class="event-item-list">
                  <% user.event_items.each do |eventitem| %>
                  <div class="event-item-div" >

                     <p class="event-item-title"><%= eventitem.item.title %></p>

                  </div>
                  <% end %>
                <% end %>
              </div>

                <% if user == current_user %>
                  <%= render 'event_items/form' %>
                <% end %>

             </div>
            <% end %>

This works and shows the title of each event item that is added to the list.

However, if I want to move the content of the P tag to a partial, I get "undefined local variable or method `eventitem'". Which doesn't make sense to me since I would assume that whatever is passed in as a partial in that loop would be treated the same as if I just left that P tag content in there. At this point, all I have in the partial would be:

<p class="event-item-title><%= eventitem.item.title %><p>

Any insight would be appreciated.

2
  • 1
    You have to pass variable eventitem to the partial. It does not magically see local vars from parent partials/templates. Commented Oct 2, 2018 at 20:05
  • guides.rubyonrails.org/… Commented Oct 2, 2018 at 20:07

1 Answer 1

3

Partials don't inherit local variables automagically, you have to define them. According to Rails Guides to pass local variables to a partial you need to set them in the locals hash

<%= render 'event_items/form', locals: {eventitem: eventitem} %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.