SinGar
SinGar

Reputation: 111

Trying to make my partial work

I need to iterate through two sets of values on the same web page so I wrote a partial to eliminate duplicate code. The two variables that I am trying to iterate through are @bookmarks and @liked_bookmarks.

The partial is rendered properly if I use either @bookmarks or @liked_bookmarks in the partial it's self but then the only one of the value sets are displayed. in the code below I tried using @resource as a stand in that will have @bookmarks and @liked_bookmarks passed into it.

this is the partial that is located in app/views/bookmarks/_bookmarksandlikes.html.erb

<div>
  <% @resource.each do |x| %>
    <%= x.url%>
  <% end %>
<div>

and this is the code for the page that I am calling the above partial on located in app/views/users/show.html.erb

<h1>My Blocmarks</h1>
<p>Find me in app/views/users/show.html.erb</p>

<div>
  <h1>Blockmarks I have created  </h1>
  <div class =row>
     <%= render partial: 'bookmarks/bookmarksandlikes', locals: { topic:     @bookmarks} %>
   </div>

</div>


<div>
  <h1>Blockmarks I have liked
    <%= render partial: 'bookmarks/bookmarksandlikes', locals: { topic:     @liked_bookmarks} %>
  </h1>
</div>

Upvotes: 0

Views: 46

Answers (3)

Pradeep Sapkota
Pradeep Sapkota

Reputation: 2082

Since you are not passing @resource into partial and passing only topic variable you need to do it

<div>
<% topic.each do |x| %>
<%= x.url%>
<% end %>
<div>

Upvotes: 2

oj5th
oj5th

Reputation: 1399

Instead of:

<div>
  <% @resource.each do |x| %>
    <%= x.url%>
  <% end %>
<div>

Do it:

<div>
  <% topic.each do |x| %>
    <%= x.url%>
  <% end %>
<div>

Because you pass the object on locals: { topic: @liked_bookmarks} you should use topic instead of using any instance that is not initialize.

Upvotes: 1

Owen
Owen

Reputation: 1547

You're close. Instead of @resource just use topic, which you are passing in as a local.

<div>
  <% topic.each do |x| %>
    <%= x.url%>
  <% end %>
<div>

Upvotes: 3

Related Questions