簡體   English   中英

Rails如何在不重新加載頁面的情況下將評論添加到微博集合中

[英]Rails How to append comments to a collection of microposts without page reload

我有兩個模型; 微博和評論。 Micropost有很多評論,而Comment屬於Micropost。

首先。 有一個StaticPagesController可以保存我的主頁操作

class StaticPagesController < ApplicationController
  def home
    if logged_in?
      @micropost = current_user.microposts.build
      @feed_items = current_user.microposts.paginate(page: params[:page])
    end
  end
(..)

home.html.erb呈現提要

= render 'shared/feed'

_feed.html.haml呈現feed_items

- if @feed_items.any?
  %ol.microposts
    = render @feed_items
  = will_paginate @feed_items

呈現_micropost.html.haml

%li
  %div.comments{data: { mid: "#{micropost.id}"}}
    %div.comment_container{:id => "comments_for_#{micropost.id}"}
      %ul
        - comments = micropost.comments
        - comments.each do |comment|
          %li 
            %a{:href => user_path(comment.user), :class => "author"}
              = comment.user.name
            %span.comment_body= comment.body
            %span.comment_timestamp= "created " + time_ago_in_words(comment.created_at).to_s
    %div
      = form_for current_user.comments.build(:micropost_id => micropost.id), |
                                                   :remote => true do |f|
        = f.hidden_field :micropost_id
        = f.hidden_field :user_id
        = f.text_field :body, class: "form-control", placeholder: "What do you think?"
        = button_tag(type: 'submit', class: "btn btn-default") do
          %i.glyphicon.glyphicon-comment
          Comment

如果提交評論,則創建操作將被調用

class CommentsController < ApplicationController
  before_action :correct_user,   only: :destroy
  def create
    @micropost = Micropost.find(params[:comment][:micropost_id])
    @comments = @micropost.comments
    @comment = current_user.comments.build(comment_params)
    @comment.save
    respond_to do |format|
      format.js
      format.html
    end
  private
    def comment_params
      params.require(:comment).permit(
        :id, :body, :user_id, :micropost_id)
    end
    def correct_user
      @comment = current_user.comments.find_by(id: params[:id])
      redirect_to root_url if @comment.nil?
    end
end

create.js.erb

var mid = $(".comment_container").parent(".comments").data('mid');
$("#comments_for_" + mid).html("<%= escape_javascript(render('comments/comment')) %>")

我的目標是在不重新加載整個頁面的情況下為其相關微博添加新評論。

我已經用%div.comments{data: { mid: "#{micropost.id}"}}將micropost.id放入標記中,並嘗試通過其父標記捕獲micropost,最后(重新)渲染評論部分地

但這總是返回相同的ID,並將每個新評論插入同一微博中。

如何獲取create.js.erb注釋的micropost.id的知識?

_comment.html.erb

<ul>
  <% @comments.each do |comment| %>
    <li>
      <a class="author" href="<%= user_path(comment.user) %>">
        <%= comment.user.name %>
      </a>
      <span class="comment_body">
        <%= comment.body %>
      </span>
      <span class="comment_timestamp">
        <%= "created " + time_ago_in_words(comment.created_at).to_s %>
      </span>
    </li>
  <% end %>
</ul>

您可以嘗試以下方法:

在您的create.js.erb

$("#comments_for_#{@comment.micropost_id}%>").html("<%= escape_javascript(render('comments/comment')) %>");

我懷疑您的jquery選擇器出了點問題,您可以更輕松地實現所需的目標。

PS:您不應在局部變量中依賴實例變量。 而是通過本地變量將實例變量傳遞給局部變量。 否則,您的局部文件將無法輕松重用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM