2016-05-20 27 views
0

我有两种模型; Micropost和评论。 Micropost有很多评论和评论属于Micropost。Rails如何在没有页面重新加载的情况下将注释添加到一组微博中

首先。有持有我家行动

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一个StaticPagesController渲染饲料

= 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')) %>") 

我的目标是一个新的评论添加到其相关的无微柱刷新整个页面。

我已经把micropost.id的标记与%div.comments{data: { mid: "#{micropost.id}"}},我试图通过它的父标签搭上微柱 最后(重新)渲染的意见与部分

但这总是返回相同的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> 
+0

这是一个很好的将数据传递给JS的导轨。 http://railscasts.com/episodes/324-passing-data-to-javascript –

+0

酷感谢提示! –

回答

1

你能尝试以下方法:

在你create.js.erb

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

我怀疑事情是不对您的jQuery选择,你可以实现你想要的更轻松。 PS:你不应该依赖你的偏好中的实例变量。相反,通过当地人将你的实例变量传递给partials。否则,你的部分不能轻易重用。

+0

you're right'$(“#comments_for _ <%= @ comment.micropost_id%>”)。html(“<%= escape_javascript(render('comments/comment'))%>”) '出来了,谢谢! –

相关问题