2016-04-12 19 views
0

我正在嘗試允許用戶使用Ajax調用在同一頁面上發表評論和刪除評論。目前用戶可以發表評論並刪除評論,但是當我在兩個視圖中都放入遠程true時,都不會重新加載頁面。如果我拿出一個遠程真實的話,這意味着一個會自動重新加載,而另一個不會。關於如何使兩個遠程trues能夠同時工作的任何建議?這是我第一次這樣做。試圖在Rails中對單獨的Ajax調用使用兩個「remote true」

這裏就是用戶可以發表評論

<%= form_for [@part, Comment.new], remote: true do |f|%> 
     <div> 
      <%= f.text_area :content %> 
      <%= f.submit "Post Comment"%> 
     </div> 
     <%end%> 

用戶如何刪除評論

<%=link_to 'delete', part_comment_path(@part, comment), data: {confirm: "Are you sure you want to delete this comment?"}, remote: true, method: :delete%> 

和create.js.erb我的第一個觀點

$('.comments').append("<%= j(render @comment) %>") 

感謝誰能解釋如何解決這個問題

UPDATE 這裏是我的意見控制器

class CommentsController < ApplicationController 
    def create 
    part = Part.find(params[:part_id]) 
    @comment = part.comments.create(comment_params.merge(user: current_user)) 

    respond_to do |format| 
     format.html {redirect_to @part} 
     format.js{} 
    end 
end 

    def destroy 
    @part = Part.find(params[:part_id]) 
    @comment = @part.comments.find(params[:id]) 
    @comment.destroy 

    respond_to do |format| 
     format.html {redirect_to @part} 
     format.js{} 
    end 
    end 

private 
def comment_params 
    params.require(:comment).permit(:content) 
    end 
end 

,這裏是我的意見路線

resources :parts do 
    resources :comments, only: [:create, :destroy] 
    end 

的情況下,這在所有

這裏幫助了是我的日誌以及

ActionView::Template::Error (No route matches {:action=>"destroy", :controller=>"comments", :id=>"52", :part_id=>nil} missing required keys: [:part_id]): 
    1: <li><%= comment.content%> by: <%= comment.user.first_name %> </li> 
    2: <% if logged_in? %> 
    3: <%=link_to 'delete', part_comment_path(@part, comment), remote: true, data: {confirm: "Are you sure you want to delete this comment?"}, method: :delete%> 
    4: <%end%> 
    app/views/comments/_comment.html.erb:3:in `_app_views_comments__comment_html_erb___3749430301288060826_70216407028000' 
    app/views/comments/create.js.erb:1:in `_app_views_comments_create_js_erb___1584098095545825378_70216383980080' 

這似乎是問題....我不聯合明確如何缺少part_id。在我的日誌錯誤

截圖

enter image description here

_comment.html.erb視圖

<li><%= comment.content%> by: <%= comment.user.first_name %> </li> 
<% if logged_in? %> 
<%=link_to 'delete', part_comment_path(@part, comment), remote: true, data: {confirm: "Are you sure you want to delete this comment?"}, method: :delete%> 
<%end%> 

回答

0

試試這個:

def destroy 
    @part = Part.find(params[:part_id]) 
    @comment = Comment.find(params[:id]) 
    if @comment.destroy 
    respond_to do |format| 
     format.html 
     format.js 
    end 
    end 
end 

如果未找到part_id然後試試這個:

<%=link_to 'delete', part_comment_path(id: comment.id), remote: true, data: {confirm: "Are you sure you want to delete this comment?"}, method: :delete%> 


def destroy 
    @comment = Comment.find(params[:id]) 
    if @comment.destroy 
    respond_to do |format| 
     format.html 
     format.js 
    end 
    end 
end 
+0

我已經有respond_to do | format |在我的評論控制器中創建和銷燬方法 – Dan

+0

與控制器和路線一起共享日誌。 –

+0

我剛剛分享了這些日誌 – Dan

相關問題