1

Using rails, how can I loop through an array defined in my controller, create a partial for each item, and access each item's value in the partial?

The Controller

Define an array for my "grapher" page:

class StaticPagesController < ApplicationController
  def home
  end

  def grapher
    @available_graphs = ["pie.png", "line.png", "lineplusbar.png"]
  end
end

The page View

In my grapher.html.erb view, I want to loop through my array and create a partial for each item.

<%= render :partial => 'graphPreviewItem', :collection => @available_graphs %>

The partial View

How can I access the array item inside of the partial?

<div class="thumbnailContainer">
    <a class="thumbnail" href="#">
        <%= image_tag "this should be the array item value" %>
    </a>
</div>
0

4 Answers 4

3

From the documentation:

When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is _product, and within the _product partial, you can refer to product to get the instance that is being rendered.

So in your case that would be (please note that in the ruby naming conventions we use snake-cased partial names):

<%= render :partial => 'graph_preview_item', :collection => @available_graphs %>

# _graph_preview_item.html.erb
<div class="thumbnailContainer">
    <a class="thumbnail" href="#">
        <%= image_tag graph_preview_item %>
    </a>
</div>

Or, more explicitly you can use the :as to choose a name of your liking. Like so:

<%= render :partial => 'graph_preview_item', :collection => @available_graphs, :as => :graph %>

# _graph_preview_item.html.erb
<div class="thumbnailContainer">
    <a class="thumbnail" href="#">
        <%= image_tag graph %>
    </a>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

So there are 2 ways :-

1) Just access it as available_graph

# _graph_preview_item.html.erb
<div class="thumbnailContainer">
  <a class="thumbnail" href="#">
    <%= image_tag available_graph %>
  </a>
</div>

2) You can use as option to access it by some other name

<%= render :partial => 'graphPreviewItem', :collection => @available_graphs, :as => :graph %>

Then access it by name graph

# _graph_preview_item.html.erb
<div class="thumbnailContainer">
  <a class="thumbnail" href="#">
    <%= image_tag graph %>
  </a>
</div>

Comments

1

According to section 3.4.4 of the canonical Rails guides:

To use a custom local variable name within the partial, specify the :as option in the call to the partial. With this change, you can access an instance of the collection as the [a] local variable within the partial.

So, pass your @available_graphs instance variable as a collection to your partial. The partial will render for each member of your collection (in your case, an array of strings). Within each partial, the collection member being iterated through is accessible via the item local variable:

# grapher.html.erb
<%= render :partial => 'graph_preview_item', :collection => @available_graphs, :as => :item %>

# _graph_preview_item.html.erb
<div class="thumbnailContainer">
    <a class="thumbnail" href="#">
        <%= image_tag item %>
    </a>
</div>

Comments

0
<%= render :partial => 'graphPreviewItem', :collection => @available_graphs, :as => image_val %> 

<div class="thumbnailContainer">
    <a class="thumbnail" href="#">
        <%= image_tag image_val %>
    </a>
</div>

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.