我正在尝试在我的评论表单提交中添加ajax,当我尝试渲染部分时我遇到了错误,我不知道如何解决它。我已经正确设置了一切,并且评论得到了很好的创建。但后来我尝试渲染部分注释,我得到了这个错误:
undefined local variable or method `each' for #<#<Class:0xae4d760>:0xae59a78>
我的 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") %>")
当我尝试渲染statuses/comments
时导致错误。
这是我的部分:
<% @comments.each do |comment| %>
<div class="com_con">
<%= Rinku.auto_link(comment.content).html_safe %>
</div>
<% end %>
然后我尝试传递像这样的变量
$("#comments_wrap").html("<%= escape_javascript(render :partial => "statuses/comments", :locals => {:comment => comment}) %>")
但它会出现此错误
undefined local variable or method `each' for #<#<Class:0xae4d760>:0xae59a78>
不确定我在这里缺少什么,我确定它很小。任何人都可以帮助我吗?
查看
<% if member_signed_in? %>
<div id="comm_form_wrap">
<%= render "comment_form" %>
</div>
<div id="comments_wrap">
<%= render "comments" %>
</div>
<% end %>
** **编辑
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
日志
Processing by StatusesController#show as HTML
Parameters: {"id"=>"86"}
[1m[35mMember Load (1.0ms)[0m SELECT "members".* FROM "members" WHERE "members"."user_name" IS NULL LIMIT 1
[1m[36mStatus Load (0.0ms)[0m [1mSELECT "statuses".* FROM "statuses" WHERE "statuses"."id" = ? LIMIT 1[0m [["id", "86"]]
[1m[35mComment Load (2.0ms)[0m SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = 86 AND "comments"."commentable_type" = 'Status' ORDER BY created_at desc LIMIT 15 OFFSET 0
[#<Comment id: 82, content: "and why not try again ha", commentable_id: 86, commentable_type: "Status", member_id: 1, created_at: "2014-06-26 06:27:05", updated_at: "2014-06-26 06:27:05">]
[1m[36mMember Load (1.0ms)[0m [1mSELECT "members".* FROM "members" WHERE "members"."id" = 1 LIMIT 1[0m
[1m[35mCACHE (0.0ms)[0m SELECT "members".* FROM "members" WHERE "members"."id" = 1 LIMIT 1
[1m[36m (0.0ms)[0m [1mSELECT COUNT(*) FROM "comments" WHERE "comments"."commentable_id" = 86 AND "comments"."commentable_type" = 'Status'[0m
Rendered statuses/_comment_form.html.erb (8.0ms)
[1m[35mCACHE (0.0ms)[0m SELECT "members".* FROM "members" WHERE "members"."id" = 1 LIMIT 1
Rendered statuses/_comments.html.erb (95.0ms)
Rendered statuses/show.html.erb within layouts/application (406.0ms)
Rendered layouts/_query.html.erb (108.0ms)
Rendered search/_search.html.erb (22.0ms)
Rendered layouts/_menu.html.erb (592.0ms)
Completed 200 OK in 2956ms (Views: 2312.1ms | ActiveRecord: 10.0ms | Solr: 0.0ms)
答案 0 :(得分:3)
问题是你的部分是在呼叫@comments.each
:
<% @comments.each do |comment| %>
2个问题:
@comments
不存在- 部分需要使用
醇>local
变量(他们不能依赖@instance
变量)
-
<强>局部模板强>
你最好这样做:
<%= render partial"statuses/comments", collection: @comments, as: comment %>
在Rails&#39;中有一个鲜为人知的功能。部分允许你基本上&#34;重新加载&#34; each member of a collection的部分内容。
这很重要的原因是因为它从你的部分中删除了很多代码。如果你使用我上面发布的部分内容,你只需要里面的部分代码:
#app/views/statuses/_comments.html.erb
<div class="com_con">
<%= Rinku.auto_link(comment.content).html_safe %>
</div>
如果您设置了正确的@instance variable
,并将其传递给collection
的{{1}}选项,Rails基本上会在循环中重新加载部分,就像您使用{{partial
一样1}}现在循环
这也适用于单一项目:
.each
-
实例变量
第二个问题是您<%= render partial: "statuses/comments", object: @comment, as: comment %>
在您的控制器中,您没有设置instance variable
实例变量。这意味着您无法将此变量的内容加载到视图中,从而导致像您一样的问题
解决此问题的方法非常简单 - 使用您在控制器中设置的@comments
!
-
<强>逃逸强>
您可能还需要查看JS中的escape
quotes:
@instance variables
我不确定这是否适用于这种情况,但是我做知道你是否将引号封装在另一个集合中,你会从你的JS中得到错误
答案 1 :(得分:0)
上面的答案帮助我解决了这个问题。将注释呈现为集合帮助我通过ajax呈现部分,我还需要在@comments
中的create
操作中定义comments_controller
以确保它不会呈现空白。