简体   繁体   English

Rails 4.2.1-使用更新HTML <render …> 不起作用

[英]Rails 4.2.1 - update html with <render …> doesn't work

I'm trying to update comments after submitting a new one via AJAX. 通过AJAX提交新评论后,我正在尝试更新评论。 Comments are rendered with a partial. 注释以部分形式呈现。 Comments are definitely submitted correctly, since they show up when I refresh the page. 注释绝对正确提交,因为它们在我刷新页面时显示。 After lots of trial and error I could limit the erroneous code to the render action within the html that I'm trying to update. 经过大量的试验和错误之后,我可以将错误代码限制为我要更新的html中的render操作。

Controller 控制者

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 create.js.erb

$('.allcommentsof<%= @Comment.post_id %>').html(<%= render :partial => "comment", :locals => {:id => post.id} %>);

div I'm trying to update div我正在尝试更新

<div class="allcommentsof<%= post.id %>">
    <%= render :partial => 'comment', :locals => {:id => post.id} %>
</div>

partial _comment.html.erb in the same folder as create.js.erb 与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>

I'd welcome any idea you have for making this work 我欢迎您有任何想法进行这项工作

log 日志

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)

I've escaped and cleaned up your create.js.erb file. 我已经逃脱并清理了您的create.js.erb文件。 You are missing the @Comment object in your locals. 您在本地人中缺少@Comment对象。

$('.allcommentsof<%= @Comment.post.id %>').html("<%= j render('comment', id: @Comment.post.id) %>");

If this doesn't fix your problem could you please provide your log file. 如果这不能解决您的问题,请提供您的日志文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM