Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Dario Hunt
2,245 PointsUndefined local variable or method ajax render partial rails
I'm trying to add ajax to my comment form submission and I've run into an error when I'm trying to render a partial and I don't know how to solve it. I have everything set up properly and comments get created fine. But then I try to render the partial for the comments and I get this error:
undefined local variable or method `each' for #<#<Class:0xae4d760>:0xae59a78>
My create.js.erb
$("#comm_form_wrap").html("<%= escape_javascript(render :partial => "statuses/comment_form") %>");
$('#comment_box').val('');
$("#comments_wrap").html("<%= escape_javascript(render :partial => "statuses/comments") %>")
When I try to render statuses/comments
is causing the error.
Here's my partial:
<% @comments.each do |comment| %>
<div class="com_con">
<%= Rinku.auto_link(comment.content).html_safe %>
</div>
<% end %>
So then I tried passing the variables like this
$("#comments_wrap").html("<%= escape_javascript(render :partial => "statuses/comments", :locals => {:comment => comment}) %>")
but it gives this error
undefined local variable or method `each' for #<#<Class:0xae4d760>:0xae59a78>
Not sure what I'm missing here, I'm sure it's something small. Can anyone help me?
View
<% if member_signed_in? %>
<div id="comm_form_wrap">
<%= render "comment_form" %>
</div>
<div id="comments_wrap">
<%= render "comments" %>
</div>
<% end %>
*EDIT*
comments_controller.rb
class CommentsController < ApplicationController
before_filter :authenticate_member!
before_filter :load_commentable
before_filter :find_member
def index
redirect_to root_path
end
def new
@comment = @commentable.comments.new
end
def create
@comment = @commentable.comments.new(params[:comment])
@comment.member = current_member
respond_to do |format|
if @comment.save
format.html { redirect_to :back }
format.json
format.js
else
format.html { redirect_to :back }
format.json
format.js
end
end
end
def destroy
@comment = Comment.find(params[:id])
respond_to do |format|
if @comment.member == current_member || @commentable.member == current_member
@comment.destroy
format.html { redirect_to :back }
else
format.html { redirect_to :back, alert: 'You can\'t delete this comment.' }
end
end
end
private
def load_commentable
klass = [Status, Medium, Project, Event, Listing].detect { |c| params["#{c.name.underscore}_id"] }
@commentable = klass.find(params["#{klass.name.underscore}_id"])
end
def find_member
@member = Member.find_by_user_name(params[:user_name])
end
end
statuses_controller
def show
@status = Status.find(params[:id])
@commentable = @status
@comments = @commentable.comments.order('created_at desc').page(params[:page]).per_page(15)
@comment = Comment.new
respond_to do |format|
format.html # show.html.erb
format.json { redirect_to profile_path(current_member) }
format.js
end
end
2 Answers

Dario Hunt
2,245 PointsI solved this issue by rendering my comments as collection instead of an each
loop, then just rendered that partial in my ajax file. I also needed to define @comments
in my comments_controller
create
action as well so that it didn't render blank.

Robert Ho
Courses Plus Student 11,383 PointsSince the error is
undefined local variable or method `each' for #<#<Class:0xae4d760>:0xae59a78>
and you are calling the each method on your @comments instance variable, there might be something wrong with the variable. Why don't you copy and paste your controller logic (code in the controller) that contains that @comments instance variable or check if the @comments instance variable is being properly queried from the database.

Dario Hunt
2,245 PointsRobert Ho Alright I edited my question to include the controller code. The show action in my statuses_controller is where the @comments instance variable is located and what the view belongs to.