0

I am rendering a bunch of tables with a graph, all the same type of data repeated a number of times so I wanted to use a partial for DRY code purposes.

This is the code I call in the view:

  <%= render 'app_usage', locals: {
    metric: "Sessions", 
    new_total: @flurry_total, 
    old_total: @flurry_old_total, 
    growth: flurry_growth,
    chart_data: flurry_chart
    } %>

and in _app_usage.html.erb

<div class="col-sm-12">
  <table class="table table-striped table-condensed">
    <tbody>
      <tr><td>Metric:</td><td><%= metric %></td></tr>
...

However I get an error for the <tr><td>Metric:</td><td><%= metric %></td></tr> saying the variable metric is undefined. Why is that?

0

1 Answer 1

2

You need to use the partial syntax to invoke the ActionView::PartialRenderer with locals:

<%= render partial: 'app_usage', locals: {
    metric: "Sessions", 
    new_total: @flurry_total, 
    old_total: @flurry_old_total, 
    growth: flurry_growth,
    chart_data: flurry_chart
    } %>
Sign up to request clarification or add additional context in comments.

4 Comments

He he yes... I missed it.
But without partial syntax how Rails reach to the partial is making me crazy if I follow OP...
@ArupRakshit You can invoke a partial if you're not looking to pass your own layout or locals using the regular render syntax. See "Rendering the default case" from the link in my answer :)
Well, I read it. Then I think OP;s code will work without locals: {}.. OP needs to pass those key/values as arguments.....

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.