Rails Ajax注释创建无法正常工作

时间:2015-06-12 04:12:22

标签: javascript jquery ruby-on-rails ruby ajax

我正在尝试使用ajax请求创建注释。实际问题是注释正确提交,响应是create.js.erb也会呈现,但注释在创建后不会刷新。

comments_controller.rb


class CommentsController < ApplicationController

  def create
    @comment = Comment.new(comments_params.except(:post_id))
    @post_id = params[:comment][:post_id].to_i
    @comments = Post.find(@post_id).comments.page(params[:page])
    @comment.user_id = 1 # to be replaced with current_user
    if @comment.save
      flash[:notice] = "Commented"
    else
      flash[:alert] = "something went wrong"
    end
    respond_to do |format|
      format.html { redirect_to :back }
      format.js
    end
  end


  private

  def comments_params
    params.require(:comment).permit(:body, :commentable_id, :commentable_type, :post_id)
  end
end

评论模型commment.rb

class Comment < ActiveRecord::Base
  paginates_per 5
  belongs_to :commentable, polymorphic: true
  belongs_to :post
  belongs_to :comment
  has_many :comments, as: :commentable

  default_scope { order('created_at DESC') }

  validates :body, presence: true, length: { maximum: 1000 }
end

posts/show.html.erb

的一部分
<div class="panel-body">
    <div id="comments">
      <%= render partial: "comments/comment", collection: @comments, locals: { post_id: @post.id } %>
    </div>
    <div class="row">
      <div class="col-md-offset-4 col-md-4" id="paginator"><%= paginate @comments %></div>
    </div>
    <div>
      <%= simple_form_for(@post.comments.new, remote: true) do |f| %>
        <%= f.input :body, label: "New Comment" %>
        <%= f.input :commentable_id, as: :hidden, value: @post.id %>
        <%= f.input :commentable_type, as: :hidden, value: "Post" %>
        <%= f.input_field :post_id, as: :hidden, value: @post.id %>
        <%= f.button :submit, class: "submit-btn" %>
      <% end %>
    </div>
  </div>

_comment.html.erb

<div class="comment-section">
  <div class="comment">
    <p><%= markdown(comment.body) =%></p>
    <small><%= time_ago_in_words(comment.created_at) %> ago</small>
    <small class="comment-reply"><a href="#">Reply</a></small>
    <small class="comment-user">By: <%= "Dummy user" %></small><!-- comment.user.name -->
  </div>
  <div class="comment2">
    <%= render partial: "comments/comment", collection: comment.comments, locals: { post_id: post_id } if comment.comments.any? %>
  </div>
  <div class="comment-form">
    <%= simple_form_for(comment.comments.new, remote: true) do |f| %>
      <%= f.input :body, label: "Reply" %>
      <%= f.input :commentable_id, as: :hidden, value: comment.id %>
      <%= f.input :commentable_type, as: :hidden, value: "Comment" %>
      <%= f.input_field :post_id, as: :hidden, value: post_id %>
      <%= f.button :submit, class: "submit-btn" %>
    <% end %>
  </div>
</div>

评论/ create.js.erb

#("#comments").html("<%= escape_javascript render partial: 'comments/comment', collection: @comments, locals: { post_id: @post_id } %>");
#("#paginator").html("<%= escape_javascript paginate(@comments, remote: true).to_s %>");

0 个答案:

没有答案