I'm passing a collection to a partial:
<%= render partial: "discount_rule", collection: @programs.discount_rules, locals: { f: form } %>
the local variable f
is successfully passed to the partial.
I print out the local variables available to me in the partial:
<%= pp local_variables %>
The result I get is:
[:local_assigns, :output_buffer, :discount_rule, :discount_rule_counter, :discount_rule_iteration, :f]
I'll add a debugger:
<% debugger %>
type in discount_rule
in the terminal and get the correct object that has a field name
.
I replace the debugger with:
<%= discount_rule.name %>
So it's in the same scope as the debugger was. I get:
undefined local variable or method `discount_rule' for #<ActionView::Base:0x000000000078c8>
There are no syntax errors. I have also tried discount_rule_counter
and discount_rule_iteration
and renaming the variable name from the collection (using as: :variable_name) and get the unknown local variable or method errors. I have tried not using collections and just rendering the partial and passing the variables in an each loop. Same errors. What am I doing wrong?
This is a Rails API only app that I had to change things slightly to have one page.
CodePudding user response:
you should be using local_assigns[:discount_rule]
to access the local variables.
one could use collection: @programs.discount_rules, as: :discount_rule
as well and call discount_rule
in the view.
At the same time make sure that the data is not nil in your controller and that you are able to print the required attribute like name before passing it to partial with something like .each do |instance| p.name