ruby Rails link_to raise no route matches error only when rendering from JS

fruv7luv  于 2023-08-04  发布在  Ruby
关注(0)|答案(1)|浏览(70)

我得到了演示文稿和评论,每个演示文稿允许许多评论,这些评论可以通过回复,所以...
在我的演示节目中,我通过以下方式成功地呈现了注解:

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

字符串
这样,所有注解都使用这个base _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>


这里一切正常,演示文稿加载评论和回复正常,问题是回复的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"%>


从演示文稿中正确加载的显示视图不允许从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'


我想知道如何完全相同的文件无法通过js呈现。我真的很感谢你的帮助,这是完成我的第一个Rails项目的唯一步骤,再次感谢!

osh3o9ms

osh3o9ms1#

由于您的链接依赖于@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, ... } %>

字符串
这样,如果您未能分配三个必需的局部变量中的任何一个,错误消息将显式地告诉您缺少哪一个。

相关问题