【问题标题】:Include ajax comments in index in rails 4在 Rails 4 的索引中包含 ajax 注释
【发布时间】:2014-09-18 14:15:24
【问题描述】:

我的 rails 应用程序中有 ajax cmets,它在帖子显示中都有效,但我想添加表单以在索引页面中呈现 cmets。

我在渲染索引页面时使用了 sorl,我尝试在 post 模型中包含 cmets,如下所示:

searchable(:include => :comments) do

  code....

end

但这不会在索引页面中的帖子下方呈现 cmets,即使我在视图中有这个

 <%= 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:

定义索引

    @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

cmets_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      

cmets 模型:

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

【问题讨论】:

    标签: ajax ruby-on-rails-4 solr comments


    【解决方案1】:

    cmets 格式应该是这样的:

    <%= simple_form_for Comment.build_from(post, current_user, ""), :url => comments_path,   :remote => true do |f| %>
    

    我必须为评论分配当前帖子 ID(commentable_id) 和当前用户 id,以便 commentable_type 构建评论

    在视图中,渲染我使用的 cmets

    <%= render post.comment_threads.order('created_at desc') %>
    

    唯一的问题是渲染忽略了 div

    【讨论】:

      猜你喜欢
      • 2022-10-02
      • 2019-10-14
      • 2020-07-25
      • 1970-01-01
      • 2010-10-15
      • 2014-11-12
      • 1970-01-01
      • 2021-01-24
      • 2015-08-23
      相关资源
      最近更新 更多