2017-07-15 22 views
0

我介紹和評論,每個演示讓許多意見和這些意見可以這樣說時,渲染匹配...的Rails的link_to募集不僅路由錯誤從JS

在我的陳述顯示,我成功地使這個評論方法:

<div id= "container_comments"> 
    <%= render @presentation.comments.where(ancestry: nil) %> 
    </div> 

這種方式,所有的評論都是使用這個基礎_comment.html.erb文件中呈現

<div class="media" id="comment_<%= comment.id %>"> 
     <div class="media-left"> 
     <img src="<%= image_path("comment.png")%>" class="media-object" style="width:45px"> 
     </div> 
     <div class="media-body"> 
     <h4 class="media-heading"><%=User.find(comment.author_id).username%> <small><i>Publicado: <%=comment.created_at.strftime('%F %T') %></i></small></h4> 
     <p><%=comment.body%></p> 


     <%= link_to "Reply", new_comment_congress_category_presentation_comment_path(@congress, @category, @presentation, :parent_id => comment), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#mynewcomment"%> 

     <% replies = Comment.where(ancestry: comment) %> 
     <% if replies.any? %> 
      <% replies.each do |reply| %> 
      <div class="media"> 
       <div class="media-left"> 
       <img src="<%= image_path("reply.png")%>" class="media-object" style="width:45px"> 
       </div> 
       <div class="media-body"> 
       <h4 class="media-heading"><%=User.find(reply.author_id).username%> <small><i>Publicado: <%=reply.created_at.strftime('%F %T') %></i></small></h4> 
       <p><%=reply.body%></p> 
       </div> 
      </div> 
      <%end%> 
     <%end%> 
     </div> 
    </div> 

一切工作正常這裏,prese ntation負荷評論和回覆正常,問題是回答的的link_to:

<%= link_to "Reply", new_comment_congress_category_presentation_comment_path(@congress, @category, @presentation, :parent_id => comment), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#mynewcomment"%> 

至極從演示放映視圖correclty負載不允許從JS文件的呈現,它顯示「無路由匹配」錯誤與遺漏鍵[ :ID]。

這是我的js文件:

$("#mynewcomment").modal('hide'); 
$(".comment_title").val(''); 
$(".comment_content").val(''); 
<%if @comment.parent == nil%> 
    $("#container_comments").append('<%= j render @comment %>'); 
    $("#comment_<%= @comment.id %>").hide().fadeIn(1000); 
<%else%> 
    <%@parent = Comment.find(@comment.parent_id)%> 
    $("#comment_<%= @parent.id %>").replaceWith('<%= j render @parent %>'); 
<%end%> 

這裏是錯誤的樣子:

ActionView::Template::Error (No route matches {:action=>"new_comment", :category_id=>#<...category info here...>, :congress_id=>#<...congress info here...>, :controller=>"comments", :parent_id=>#<...parent comment info here...>, :presentation_id=>#<...Presentation info here...>}, missing required keys: [:id]): 
7:    <p><%=comment.body%></p> 
8: 
9: 
10:    <%= link_to "Responder", new_comment_congress_category_presentation_comment_path(@congress, @category, @presentation, :parent_id => comment), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#mynewcomment", :parent_id => comment %> 
11: 
12:    <% replies = Comment.where(ancestry: comment) %> 
13:    <% if replies.any? %> 

app/views/comments/_comment.html.erb:10:in 
`_app_views_comments__comment_html_erb__456499711_134033424' 
app/views/comments/create.js.erb:5:in 
`_app_views_comments_create_js_erb__184660212_140959668' 

我不知道確切的同一個文件是如何失敗的渲染throught js的....我真的對你的幫助感到滿意,這是我完成第一個鐵軌項目的唯一步驟,再次感謝!

回答

0

由於您的鏈接取決於@congress,@category@presentation變量,因此您必須確保這些相同的變量也可用於JS模板。

要犯這種錯誤很難做出的,你可以改變你的模板使用局部變量,而不是實例變量,使用locals哈希:

# in _comment.html.erb 
<%= 
    link_to "Reply", 
    new_comment_congress_category_presentation_comment_path(
     congress, category, presentation, 
     parent_id: comment 
    ) 
%> 

# in show.html.erb 
<%= 
    render @presentation.comments.where(ancestry: nil), 
    locals: { congress: @congress, category: @category, presentation: @presentation } 
%> 

# in whatever.js.erb 
<%= j render @comment, locals: { congress: @congress, ... } %> 

這樣一來,如果你不分配即使是三個必需的局部變量中的單個變量,錯誤消息也會明確告訴您缺少哪一個。