3

I get error in partial line #1: undefined local variable or method 'level'

Code in my view:

<div id="comments">
    <% level = 0%>
    <% @comments.each do |comment| %>
      <%=render partial: 'comments/single_comment', locals: {level: level, comment: comment} %>
    <% end %>
    <% if 0 < level %>
    <% (level).times do %>
    </div>
    <% end %>
    <% end %>
  </div>
</div> 

And partial first lines:

<% if comment.level < level %>
  <% (level - comment.level).times do %>
  </div>
  <% end %>
<% end %> 

Any idea what's wrong here?

9
  • Try inside your partial using @comment and @level maybe Commented Feb 12, 2013 at 18:35
  • Funny thing is comment variable in partial works, level doesn't. Is this because I set it before the block? Commented Feb 12, 2013 at 18:37
  • are u sure Comment has a column called level? Commented Feb 12, 2013 at 18:38
  • yep, 100% sure, just double checked it. But it's not comment.level problem but level variable. Commented Feb 12, 2013 at 18:39
  • try to call comment.level in your view. I believe the problem is not related to the variable... Commented Feb 12, 2013 at 18:43

3 Answers 3

2

Looks like this code should be work(it's no so good, but it should be work) I think problem that you use your partial (comments/single_comment) elsewhere in some part of code what we didn't see without 'level local' :)

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately not :(, just run grep to double check it.
can I see "Application Trace" here?)
1

seems like the code is right .. try to check if you have any partial view that use single_comment filename ... and also you can use collection instead doing a loop

<%= render partial: 'comments/single_comment', collections: @comments, locals: { level: level } %>

1 Comment

just run grep -r 'single_comment' . in views directory and it's the only place I use it (thanks for collections tip)
0

You are passing arguments to the partial in a wrong way. update it to following.

<%=render partial: 'comments/single_comment', locals: => {:level => level, :comment => comment} %>

now you can access your objects in partials as follows

<% if locals[:level] < level %>
  <% (level - locals[:level]).times do %>
  </div>
  <% end %>
<% end %> 

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.