未定义的局部变量或方法ajax呈现部分rails



我试图添加ajax到我的评论表单提交,我遇到了一个错误,当我试图渲染部分,我不知道如何解决它。我把一切都设置好了,评论也创建得很好。然后我尝试渲染部分的注释,我得到这个错误:

undefined local variable or method `each' for #<#<Class:0xae4d760>:0xae59a78>
我的<

strong> 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>

不知道我错过了什么,我肯定是一些小东西。有人能帮我吗?

<

视图/strong>

<% 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)

问题是您的局部呼叫@comments.each:

<% @comments.each do |comment| %>

2的问题:

  1. @comments不存在
  2. 局部需要使用local变量(它们不能依赖@instance变量)

,

泛音

你最好这样做:

<%= render partial"statuses/comments", collection: @comments, as: comment %> 

在Rails的partial中有一个鲜为人知的功能,它允许你基本上为集合的每个成员"重新加载"partial。

这很重要的原因是它从你的部分中删除了很多代码。如果你使用我上面发布的部分代码,你只需要在部分代码

中使用
#app/views/statuses/_comments.html.erb
<div class="com_con">
    <%= Rinku.auto_link(comment.content).html_safe %>
</div>

如果你设置了正确的@instance variable,并将其传递给partialcollection选项,Rails基本上会在循环中重新加载部分,就像你现在使用.each循环一样

这也适用于单数项:

<%= render partial: "statuses/comments", object: @comment, as: comment %>

,

第二个问题是instance variable的设置

在控制器中,没有设置@comments实例变量。这意味着您无法将该变量的内容加载到视图中,从而导致类似

的问题。

解决这个问题的方法非常简单-使用您在控制器中设置的@instance variables !

,

逃脱

你可能还需要看看如何在你的JS中escape引号:

$("#comments_wrap").html("<%= escape_javascript(render :partial => "statuses/comments", :locals => {:comment => comment}) %>")

我不确定这是否适用于这种情况,但我知道如果你封装引号在另一个集合,你会得到错误从你的JS

上面的答案帮我解决了这个问题。将注释呈现为一个集合帮助我通过ajax呈现部分,我还需要在comments_controllercreate动作中定义@comments,以确保它不会呈现空白。

相关内容

  • 没有找到相关文章

最新更新