ruby on rails 3 - RoR: Passing collection to partial won't work -
i'm extending open source ror project , have following problem: original programmers passed collection partial this
<%= render partial: "option", collection: survey.options %>
and use in partial, e.g. this
<%= option.name %>
all works fine , understandable when reading guide http://guides.rubyonrails.org/layouts_and_rendering.html
a survey has answer_pairs (with string fields "answer1" , "answer2") , try pass them partial this
<%= render partial: "table_head", collection: survey.answer_pairs %>
and try use in partial _table_head.html.erb this
<td><b><%= answer_pair.answer2 %></b></td>
but rails gives me nameerror - undefined local variable or method `answer_pair' #<#:0x007fa07b9e0098>: app/views/surveys/_table_head.html.erb:1
could tell me why, please? when pass "as:" this
<%= render partial: "table_head", collection: survey.answer_pairs, as: :answer_pair %>
it recognizes first answer_pair of collection, nothing more. recognizes answer_pairs of collection, if like
<% survey.answer_pairs.each |pair| %> <td><b><%= pair.answer2 %></b></td> <% end %>
any appreciated!
the auto generated variable named after template, not collection name. if access <td><b><%= table_head.answer2 %></b></td>
, should work expect.
Comments
Post a Comment