2016-07-27 89 views
0

我嘗試在我的rails應用程序中使用ajax呈現我的評論...但它不起作用,並且在我重定向頁面後發表評論...因此評論被成功創建但問題是使用ajax和jquery呈現註釋! 我有這行我application.html.erb:評論沒有在rails中顯示ajax

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 
<%= csrf_meta_tags %> 

,這需要在我的application.js行:

//= require jquery 
//= require jquery_ujs 

這裏是員額我show.html.erb文件:

<div id="posts" class="transitions-enabled"> 
    <div class="box panel panel-default"> 
    <%= render 'post', post: @post %> 
    </div> 
</div> 

我彥博部分:

<div class="panel-body"> 
    <%= link_to image_tag(post.user.avatar.url, size: "50x50", class: "img-circle"), profile_path(post.user.user_name) %> 
    <ul class="posts-shows"> 
    <li><strong><%= link_to post.user.user_name, profile_path(post.user.user_name) %></strong></li> 
    <li><small><%= link_to "#{time_ago_in_words(post.created_at)} ago", post_path(post), class: "time-link" %></small></li> 
    </ul> 
    <p class="disc-posts"><%= post.description %></p> 
    <%= link_to image_tag(post.image.url(:large), class: "img-responsive img-in") %><br/> 
    <% if post.user == current_user %> 
    <div><%= link_to "Edit", edit_post_path(post) %> | <%= link_to "delete", post, method: :delete, data: {confirm: "Are you sure?"} %> </div> 
    <% else %>        
    <div><%= link_to "Repost", repost_post_path(post), method: :post, data: { confirm: "Are you sure?"} if user_signed_in? %></div>     
    <% end %> 
</div> 

<div class="panel-footer"> 
    <div class="comment-form"> 
    <%= form_for([post, post.comments.build], remote: true) do |f| %> 
     <%= f.text_field :content, placeholder: 'Add a comment...', class: "form-control comment_content", id: "comment_content_#{post.id}" %> 
    <% end %> 
    </div> 
    <div class="comments" id= "comments_#{post.id}"> 
    <% if post.comments.present? %> 
     <%= render post.comments, post: post %> 
    <% end %> 
    </div> 
</div> 

我的意見的文件夾_comment.html.erb:

<% if comment.user_id.present? %> 
    <div id="comment"> 
    <%= link_to image_tag(post.user.avatar.url, size: "30x30", class: "img-circle img-comments"), profile_path(comment.user.user_name) %> 
    <div class="user-name"> 
     <%= link_to comment.user.user_name, profile_path(comment.user.user_name), class: "username-size" %> 
    </div> 
    <div class="comment-content"> 
     <%= comment.content %> 
    </div> 
    </div> 
<% end %> 

,這裏是註釋文件夾下的create.js.erb:

$('#comments_<%= @post.id %>').append("<%=j render 'comments/comment', post: @post, comment: @comment %>"); 
$('#comment_content_<%= @post.id %>').val('') 

和我的意見控制器:

class CommentsController < ApplicationController 
    before_action :set_post 

    def index 
    @comments = @post.comment.all 
    end 

    def new 
    @comment = @post.comments.build(comments_params) 
    end 

    def create 
    @comment = @post.comments.build(comments_params) 
    @comment.user_id = current_user.id 

    if @comment.save 
     respond_to do |format| 
      format.html { redirect_to root_path } 
      format.js 
     end 
    else 
     flash[:alert] = "Check the comment form, something went horribly wrong." 
     render root_path 
    end 
    end 


    def destroy 
    @comment = @post.comments.find(params[:id]) 

    @comment.destroy 
    flash[:success] = "Comment deleted :(" 
    redirect_to root_path 
    end 

    private 

    def comments_params 
    params.require(:comment).permit(:content) 
    end 

    def set_post 
    @post = Post.find(params[:post_id]) 
    end 
end 
+0

可以調試這種方式。在chrome - inspect>網絡中。提交請求並仔細查看響應。您可以重試控制檯中的響應以查看JavaScript如何運行。 – Swards

+0

我不熟悉鉻網絡和控制檯..但耶是學習的好東西..我一定會檢查一些youtube視頻...謝謝 – sam0101

+0

Chrome開發工具是你的需求:https:// developers.google.com/web/tools/chrome-devtools/iterate/inspect-styles/?hl=zh-CN – Swards

回答

1

我不認爲這條線是正確的 -

<div class="comments" id= "comments_#{post.id}"> 

我認爲你需要這個

<div class="comments" id= "comments_<%= post.id %>"> 
+0

是啊,它現在正在渲染......但我得到另一個問題......當我發表評論在一篇文章中索引我的頁腳在面板後面後面的帖子...但是當我重定向它修復問題...所以仍然有一個問題與Ajax ...你知道任何解決方案嗎? – sam0101

+0

之前所有我想感謝你的幫助:) – sam0101

+0

它可能是你的CSS。 Chrome檢查也會對此有所幫助。您可以查看元素及其樣式規則。頁腳可能在帖子頂部有很多原因。你在使用石工嗎?可能需要重新加載(如果是的話,請參閱砌體的重新加載方法)。 – Swards