我正在尝试在我的Rails 4应用程序中构建评论。我已经能够使用Railcasts 262作为指南获得嵌套注释。我想在用户想要回复评论或添加新评论并限制页面重新加载的时间时显示新的评论字段。我查看了Railscasts 136和其他人。我正在使用祖先的宝石,无法获得所需的表格和评论。
问题似乎是当我将用户发送到new_comment_path时,post_id会丢失。在添加路径中的ajax和新步骤之前,我已经能够将其与注释一起保存。在我刚刚在帖子显示页面上呈现评论表单之前,它确实有效。现在新的评论正在保存,但是post_id,ancestry_id和parent_id都是零。因此,我无法将这些评论显示在帖子的显示页面上。在我将new_comment工作后,我也会设置它以便“回复”也可以。如果您有任何建议,请告诉我。感谢。
post.rb
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments
comment.rb
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :post
has_ancestry
end
_comment_form.html.erb
<%= form_for @comment, remote: true do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="comment_field">
<%= f.hidden_field :post_id, :value => @comment.post_id %>
<%= f.hidden_field :parent_id, :value => params[:parent_id] %>
<%= f.text_area :content, placeholder: "" %>
</div>
<button class="btn" type="submit">
Send request
</button>
<% end %>
发布show.html.erb
... information about post ...
<div id="added_comments">
<%= link_to "New Comment", new_comment_path, id: "new_link", remote: true, :post => @post %>
</div>
<%= nested_comments @post.comments.arrange(:order => :created_at) %>
评论new.html.erb
<h1>New Comment</h1>
<%= render 'comment/form' %>
评论new.js.erb
$('#new_link').hide().after('<%= j render("form") %>');
create.js.erb
$('#new_comment').remove();
$('#new_link').show();
$('#added_comments').append('<%= j nested_comments (@post.comments).arrange(:order => :created_at) %>');
_comment.html.erb
<%= link_to comment.user.name, comment.user %>
<%= comment.content %>
<%= link_to "Reply", post_path(@post, :parent_id => comment) %>
comments_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
posts_controller.rb
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
答案 0 :(得分:0)
你的帖子显示模板有点不对劲。当您将任意符号传递给link_to时,它将成为链接标记上的html属性,因此当您使用
时:post => @post
在您的html中变成类似
的内容post="#<Post:0x007fe26e0907d8>"
因为您希望将parent_id和post_id作为参数传递给新的评论操作而无法为您工作。相反,你可以尝试这样的东西
<%= 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 %>
另外,您可能刚刚省略了文件,但是从我看到的注释中,new.js.erb模板需要引用comment_form而不是表单,因为这就是您为模板命名的内容