我在通过AJAX提交新评论后尝试更新评论。注释以部分方式呈现。评论肯定是正确提交的,因为它们会在我刷新页面时显示。经过大量的反复试验,我可以将错误的代码限制在我试图更新的html中的渲染操作中。
控制器
class CommentsController < ApplicationController
def create
@Comment = Comment.new(comments_params)
@Comment.save
respond_to(:js)
end
private
def comments_params
params.require(:comment).permit(:post_id, :author, :body, :value, :parent_id)
end
end
create.js.erb
$('.allcommentsof<%= @Comment.post_id %>').html(<%= render :partial => "comment", :locals => {:id => post.id} %>);
div我试图更新
<div class="allcommentsof<%= post.id %>">
<%= render :partial => 'comment', :locals => {:id => post.id} %>
</div>
与create.js.erb
在同一文件夹中的部分_comment.html.erb<% @Comments = Comment.where(:post_id => id, :parent_id => nil).order("value DESC") %><br>
<% @Comments.each do |c| %>
<div><%= c.author %>: <%= c.body %> <span class="badge">$<%= c.value %></span><span class="glyphicon glyphicon-remove" style="font-size: 10px"></span></div>
<div class="subcomms">
<% @Subcomms = Comment.where(:parent_id => c.id) %>
<% @Subcomms.each do |s| %>
<%= s.author %>: <%= s.body %> <span class="badge">$<%= s.value %></span><span class="glyphicon glyphicon-remove" style="font-size: 10px"></span>
<% end %>
<%= form_for Comment.new, url: comments_create_path do |d| %>
<%= d.text_field :body, :autocomplete => false %>
<%= d.hidden_field :author, :value => current_user.username %>
<%= d.hidden_field :post_id, :value => id %>
<%= d.hidden_field :value, :value => 0 %>
<%= d.hidden_field :parent_id, :value => c.id %>
<%= d.submit "Submit", style: "opacity: 0" %>
<% end %>
</div>
我欢迎你有任何想法来完成这项工作
日志
Started POST "/comments/create" for 79.225.166.124 at 2015-08-06 20:22:32 +0000
Cannot render console from 79.225.166.124! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by CommentsController#create as JS
Parameters: {"utf8"=>"✓", "comment"=>{"body"=>"test", "author"=>"Lenco", "post_id"=>"99", "value"=>"0", "parent_id"=>""}}
User Load (27.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(8.4ms) SELECT "posts"."id" FROM "posts" WHERE "posts"."author_id" = ? [["author_id", 1]]
DEPRECATION WARNING: Calling #any_of directly is deprecated and will be removed in activerecord_any_of-1.2.
Please call it with #where : User.where.any_of(cond1, cond2). (called from get_offer at /home/ubuntu/workspace/app/controllers/application_controller.rb:12)
(45.8ms) SELECT DISTINCT post_id FROM "offers" WHERE (("offers"."buyer_id" = 1 OR "offers"."post_id" IN (21, 22, 23, 24, 29, 57, 60, 64, 80, 81))) ORDER BY amount DESC
(14.8ms) SELECT SUM("users"."wealth") FROM "users"
(6.2ms) SELECT SUM("posts"."value") FROM "posts"
(1.7ms) SELECT "users"."id", "users"."wealth" FROM "users"
(3.3ms) SELECT SUM("posts"."value") FROM "posts" WHERE "posts"."author_id" = ? [["author_id", 1]]
(0.1ms) SELECT SUM("posts"."value") FROM "posts" WHERE "posts"."author_id" = ? [["author_id", 3]]
(1.0ms) SELECT SUM("posts"."value") FROM "posts" WHERE "posts"."author_id" = ? [["author_id", 10]]
(0.1ms) SELECT SUM("posts"."value") FROM "posts" WHERE "posts"."author_id" = ? [["author_id", 11]]
User Load (5.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 11]]
(2.4ms) begin transaction
SQL (9.3ms) INSERT INTO "comments" ("post_id", "author", "body", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["post_id", 99], ["author", "Lenco"], ["body", "test"], ["value", 0], ["created_at", "2015-08-06 20:22:33.143458"], ["updated_at", "2015-08-06 20:22:33.143458"]]
(44.2ms) commit transaction
Comment Load (7.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? AND "comments"."parent_id" IS NULL ORDER BY value DESC [["post_id", 99]]
Comment Load (7.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_id" = ? [["parent_id", 90]]
Comment Load (2.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_id" = ? [["parent_id", 94]]
Comment Load (1.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_id" = ? [["parent_id", 95]]
Comment Load (2.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_id" = ? [["parent_id", 96]]
Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_id" = ? [["parent_id", 97]]
Rendered comments/_comment.html.erb (228.2ms)
Rendered comments/create.js.erb (359.3ms)
Completed 200 OK in 1145ms (Views: 409.2ms | ActiveRecord: 191.5ms)
答案 0 :(得分:0)
我已经转义并清理了您的create.js.erb文件。您缺少本地人的@Comment对象。
rootElement.Elements("_char").Attributes("data_tag");
如果这没有解决您的问题,请提供您的日志文件。