我在我的rails应用程序中有ajax注释,它们都可以在帖子中显示,但我想添加表单以在索引页面中呈现注释。
我在渲染索引页面时使用了sorl,我试图在帖子模型中包含注释,如下所示:
searchable(:include => :comments) do
code....
end
但是这不会在索引页面中的帖子下面呈现评论,即使我在视图中有这个评论
<%= render :partial => 'comments/comment',:collection => @comments, :as => :comment %>
为了帮助您理解,以下是所有代码:
_post.html.erb:
<%= simple_form_for [post, Comment.new], :url => comments_path, :remote => true do |f| %>
<%= image_tag current_user.avatar.url(:small) %>
<%= f.input :body, placeholder: 'write a comment...', :input_html => { :rows => "1"}, :label => false %>
//the below two lines do not work, the commentable_id is 0 and commentable_type is blank
//post.comments.commentable_id doesn't work either
<%= f.input :commentable_id, :as => :hidden, :value => Comment.new.commentable_id %>
<%= f.input :commentable_type, :as => :hidden, :value => Comment.new.commentable_type %>
<%= button_tag(type: 'submit', :id => 'commentsend') do %>
<i class="fa fa-check"></i>
<% end %>
<% end %>
<%= render :partial => 'comments/comment',:collection => @comments, :as => :comment %>
Posts_controller:
def index
@user_count = User.where(:school => current_user.school).count
@blub_count = Post.where(:category => "status").where(:school => current_user.school).count
@items_count = Post.where(:school => current_user.school).where.not( :category => "status").count
@search = Post.search(:include => :comments) do #Post.search do
fulltext params[:search]
with(:school, current_user.school)
paginate :page => params[:page], :per_page => 20
order_by(:updated_at, :desc)
end
@posts = @search.results
respond_to do |format|
format.js
format.html
end
end
def show
@post = Post.find(params[:id])
@comments = @post.comment_threads.order('created_at desc')
@new_comment = Comment.build_from(@post, current_user, "")
end
comments_controller:
def create
@comment_hash = params[:comment]
@obj = @comment_hash[:commentable_type].constantize.find(@comment_hash[:commentable_id])
# Not implemented: check to see whether the user has permission to create a comment on this object
@comment = Comment.build_from(@obj, current_user.id, @comment_hash[:body])
if @comment.save
render :partial => "comments/comment", :locals => { :comment => @comment }, :layout => false, :status => :created
else
render :js => "alert('error saving comment');"
end
end
评论模型:
acts_as_nested_set :scope => [:commentable_id, :commentable_type]
validates :body, :presence => true
#validates :user, :presence => true
# NOTE: install the acts_as_votable plugin if you
# want user to vote on the quality of comments.
#acts_as_votable
belongs_to :commentable, :polymorphic => true
# NOTE: Comments belong to a user
belongs_to :user
belongs_to :post
# Helper class method that allows you to build a comment
# by passing a commentable object, a user_id, and comment text
# example in readme
def self.build_from(obj, user_id, comment)
new \
:commentable => obj,
:body => comment,
:user_id => user_id
end
#helper method to check if a comment has children
def has_children?
self.children.any?
end
# Helper class method to lookup all comments assigned
# to all commentable types for a given user.
scope :find_comments_by_user, lambda { |user|
where(:user_id => user.id).order('created_at DESC')
}
# Helper class method to look up all comments for
# commentable class name and commentable id.
scope :find_comments_for_commentable, lambda { |commentable_str, commentable_id|
where(:commentable_type => commentable_str.to_s, :commentable_id => commentable_id).order('created_at DESC')
}
# Helper class method to look up a commentable object
# given the commentable class name and id
def self.find_commentable(commentable_str, commentable_id)
commentable_str.constantize.find(commentable_id)
end
答案 0 :(得分:0)
评论形式应该是这样的:
<%= simple_form_for Comment.build_from(post, current_user, ""), :url => comments_path, :remote => true do |f| %>
我必须为评论指定当前帖子ID(commentable_id)和当前用户id为commentable_type来构建评论
在视图中,以呈现我使用的评论
<%= render post.comment_threads.order('created_at desc') %>
唯一的问题是渲染是忽略div