2016-05-20 25 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'))%>」) '出來了,謝謝! –

相關問題