从单独的视图文件夹中引用一个视图文件夹中的 div id

发布于 2025-01-06 23:15:12 字数 658 浏览 0 评论 0原文

我使用不显眼的 javascript 将代码从部分附加到 div 块。在我的视图中有两个文件夹,分别称为视频和帖子。在视频文件夹中有一个show.html.erb 文件。在此 show.html.erb 文件中,有一个允许用户创建帖子的表单。表单属性 Remote 设置为 true,我希望将表单提交的帖子加载到页面(相同的 show.html.erb 页面)而无需刷新(AJAX 功能)。提交表单后,它会被发送到 posts 控制器的 create 方法。在这个方法中有一个

format.js

调用views>帖子>创建.js.erb。
到目前为止,我为此文件编写的代码是:

$('#comments').append('<%=escape_javascript(render(:partial => 'posts'))%>');

comments 是 show.html.erb 文件中块的 div id。部分“帖子”位于帖子文件夹内(views > posts > _posts)。现在这无法正常工作,我相信这是因为 create.js.erb 文件位于与试图引用其 div id 的文件不同的文件夹中。如何从 posts 文件夹中获取 javascript 代码以引用视频显示文件中的 div 块?谢谢。

I am appending code to a div block from a partial using unobtrusive javascript. Inside my views there are two folders called videos and posts. In the the video folder is a show.html.erb file. In this show.html.erb file there's a form to allow a user to create a post. The form attribute remote is set to true and I want the post that gets submitted by the form to be loaded onto the page (the same show.html.erb page) without having to refresh (AJAX functionality). When the form is submitted it is sent to the create method of the posts controller. In this method there's a

format.js

that calls views > posts > create.js.erb.
The code I have written for this file so far is:

$('#comments').append('<%=escape_javascript(render(:partial => 'posts'))%>');

comments is the div id for a block in the show.html.erb file. The partial 'posts' is inside the posts folder (views > posts > _posts). Right now this is not working properly and I believe its because the create.js.erb file is in a separate folder from the file whose div id its trying to reference. How do I get the javascript code from the posts folder to reference the div block inside the video show file? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追风人 2025-01-13 23:15:12

我不确定这是否是您所要求的,但您可以像这样传递部分完整路径:

 $('#next_videos').empty().append('<%=escape_javascript(render(:partial => 'videos/partial_name'))%>');

I'm not sure if this is what you're asking but you can pass a partial the full path like this:

 $('#next_videos').empty().append('<%=escape_javascript(render(:partial => 'videos/partial_name'))%>');
浪推晚风 2025-01-13 23:15:12

作为 Rails 的新手,甚至是 jquery 的新手,我在识别问题时遇到了问题。我认为因为我的 jquery 代码位于与 div 不同的文件夹中,所以我想引用上面的代码无法正常工作。答案(正如我发现的)是,只要 div 和 jquery 位于同一个 DOM 中,引用就可以正常工作。我的代码中其他地方有一个错误,导致 AJAX 请求无法正常运行。修复后我的 jquery 代码按预期工作。谢谢

Being relatively new to rails and even newer to jquery I had an issue in identifying my problem. I thought that because my jquery code was in a separate folder from the div I wanted to reference that the code I had above wouldn't work properly. The answer (as I found out) is that as long as the div and the jquery are in the same DOM the reference works fine. I had a bug somewhere else in my code that was preventing the AJAX request from functioning normally. After fixing that my jquery code works as intended. Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文