Jquery 使用Ajax和祖先gem的嵌套注释
我正试图在Rails 4应用程序中添加评论。我已经能够使用Railcasts 262作为指南来获得嵌套注释。我想在用户想要回复评论或添加新评论时显示新评论字段,并限制页面重新加载的次数。我看过《铁路诈骗案136》和其他一些。我正在使用祖先宝石,无法获得所需的表单和注释 问题似乎是,当我将用户发送到新的\u comment\u路径时,post\u id丢失了。在添加ajax和路由中的新步骤之前,我已经能够将它与注释一起保存。之前,我只是在post show页面上呈现评论表单,它工作正常。现在新的注释被保存,但是post_id、祖先id和父id都是零。因此,我无法将这些评论呈现在帖子的显示页面上。在我得到新的_评论后,我还将对其进行设置,以便“回复”也能正常工作。如果你有任何建议,请告诉我。谢谢 post.rbJquery 使用Ajax和祖先gem的嵌套注释,jquery,ruby-on-rails,comments,ancestry,Jquery,Ruby On Rails,Comments,Ancestry,我正试图在Rails 4应用程序中添加评论。我已经能够使用Railcasts 262作为指南来获得嵌套注释。我想在用户想要回复评论或添加新评论时显示新评论字段,并限制页面重新加载的次数。我看过《铁路诈骗案136》和其他一些。我正在使用祖先宝石,无法获得所需的表单和注释 问题似乎是,当我将用户发送到新的\u comment\u路径时,post\u id丢失了。在添加ajax和路由中的新步骤之前,我已经能够将它与注释一起保存。之前,我只是在post show页面上呈现评论表单,它工作正常。现在新的注
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments
posts\u controller.rb
def new
@comment = Comment.new(:parent_id => params[:parent_id], :post_id => params[:post_id])
end
def create
@comment = Comment.create(comment_params)
@comment.user = current_user
if @comment.save
CommentMailer.comment_confirmation(@comment).deliver
flash[:success] = "Comment added."
respond_to do |format|
format.html { redirect_to :back }
format.js
end
else
flash[:error] = "Comment cannot be blank"
end
end
def show
@comment = Comment.find(params[:id])
@user = @comment.user
end
def show
@post = Post.find(params[:id])
@comment = @post.comments.build(:parent_id => params[:parent_id])
@user = User.find(@post.user_id)
@comment.user_id = current_user
end
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = "Post added!"
redirect_to root_url
else
@repository_items = [ ]
render 'shared/_post_form'
end
end
你的帖子显示模板有点错误。当您传递一个任意符号以链接时,它将成为链接标记上的html属性,因此当您使用
:post => @post
在您的html中,它会变成
post="#<Post:0x007fe26e0907d8>"
<%= @post.comments.each do |comment| %>
<%= link_to "Reply to Comment", new_comment_path(parent_id: comment.id, post_id: @post.id), id: "new_link", remote: true %>
<%- end %>
post=“#”
这对您不起作用,因为您希望将parent_id和post_id作为参数传递给新的comment操作。你可以试试这样的
<%= link_to "New Comment", new_comment_path(parent_id: @post.id, post_id: @post.id), id: "new_link", remote: true %>
然后,当你去实现你的评论回复时,你会有这样的结果
post="#<Post:0x007fe26e0907d8>"
<%= @post.comments.each do |comment| %>
<%= link_to "Reply to Comment", new_comment_path(parent_id: comment.id, post_id: @post.id), id: "new_link", remote: true %>
<%- end %>
另外,您可能刚刚省略了文件,但从我看到的注释new.js.erb模板需要引用注释表单而不是表单,因为这是您命名模板的内容
def show
@post = Post.find(params[:id])
@comment = @post.comments.build(:parent_id => params[:parent_id])
@user = User.find(@post.user_id)
@comment.user_id = current_user
end
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = "Post added!"
redirect_to root_url
else
@repository_items = [ ]
render 'shared/_post_form'
end
end
:post => @post
post="#<Post:0x007fe26e0907d8>"
<%= link_to "New Comment", new_comment_path(parent_id: @post.id, post_id: @post.id), id: "new_link", remote: true %>
<%= @post.comments.each do |comment| %>
<%= link_to "Reply to Comment", new_comment_path(parent_id: comment.id, post_id: @post.id), id: "new_link", remote: true %>
<%- end %>