Rails 在渲染后强制重新加载 javascript [英] Rails force reload of javascript after render
问题描述
我在我的 rails 应用程序中使用了一个表单,最后在我的控制器中我检查表单的内容是否正确,如果不是,我再次渲染页面并解释错误
控制器的一部分
如果@insertion.save重定向到@insertion别的呈现新"结尾
在我看来,我用这一行来显示错误.
<% if @insertion.errors.any?%><div id="error_explanation"><div class="alert alert-danger">该表单包含 <%=pluralize(@insertion.errors.count, "error") %>.
<ul><% @insertion.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>
<%结束%>
问题是渲染后页面不会再次加载我的 javascript 文件,因此视图与我预期的不一样
渲染后如何强制重新加载js?
由于turbolink,这是我的咖啡文件
$(document).on "turbolinks:load", ->插入JS = 新插入()如果 $('.insertions.show').length >0控制台日志(插入.显示")插入JS.showJS() .....继续
Turbolinks by design JavaScript 和 CSS 不会重新加载,它将您的链接等转换为 XHR 请求,然后使用响应来更新 并触发它的特殊事件(这也是为什么 jQuery
$(function() { ... });
与 Turbolinks 不按预期工作.由于它执行 XHR,它不是真正的页面加载,即使它看起来像 Rails 控制器.
在大多数情况下,您可以像您展示的那样使用 turbolinks:load
事件,但这不是一个神奇的解决方案,您必须仍然记住所有旧"JavaScript 和 HTML 仍然存在,即任何 JavaScript全局变量"都不会被重置,等等.
如果您想在每个页面上重新加载/执行 JavaScript,只需从您的应用程序中删除 Turbolinks(Gemfile
、application.js
).如果你为你的 CSS/JS 实现了适当的缓存头并且没有太多的 JS,那么性能差异不会那么大.
如果您出于某种原因只想为特定页面/链接/表单禁用它,您可以添加 data-turbolinks="false"
属性 到链接.请记住,这需要在指向页面的链接上,而不是页面本身.为了让页面本身真正重新加载,您可以像 location.reload()
那样做,但请记住,这是在渲染/重定向后重新加载完整的单独页面.
如果你真的想强制重新加载 JavaScript,你可以动态地插入另一个 到 head 中,但记住你的原始 JavaScript 仍然加载,所以那可能会很痛苦.
请注意,Turbolinks 实际上非常庞大和复杂,因此值得阅读它自己的文档,而不仅仅是来自 Rails 的小片段.https://github.com/turbolinks/turbolinks
I'm using a form in my rails application, in my controller at the end I check if the content of the form is ok or not, if not I render the page again explaining on top the errors
part of controller
if @insertion.save
redirect_to @insertion
else
render 'new'
end
and in my view I've got this line to show the errors.
<% if @insertion.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@insertion.errors.count, "error") %>.
</div>
<ul>
<% @insertion.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
the problem is that after the render the page will not load again my javascript file, so the view isn't as I expected
How can I force to reload js after render?
this is my coffee file due to turbolink
$(document).on "turbolinks:load", ->
insertionJS = new Insertion()
if $('.insertions.show').length > 0
console.log("insertions.show")
insertionJS.showJS() .....continue
With Turbolinks by design JavaScript and CSS are not reloaded, it turns your links etc. into XHR requests then uses the response to update <body>
and trigger its special events (also why the jQuery $(function() { ... });
does not work as intended with Turbolinks. Since its doing an XHR, its not a real page load at all, even if it looks like one to the Rails controller.
For most cases you can use turbolinks:load
event like you showed, but that is not a magix solution, and you must still remember that all your "old" JavaScript and HTML still exists, that any JavaScript "globals" wont be reset, etc.
If you want to be reloading/executing JavaScript on every page, just remove Turbolinks from your application (Gemfile
, application.js
). If you implement proper cache headers for your CSS/JS and dont have too much JS, the performance difference is not that big.
If you just want to disable it for a specific page/link/form for some reason, you can add the data-turbolinks="false"
attribute to the link. Remember this needs to be on the link leading to the page, not the page itself. For the page itself to really reload, you could do like location.reload()
, but remember thats a complete seperate page reload after your render/redirect.
If you really want to force JavaScript to reload, you could insert another <script>
into head dynamically, but remember that your original JavaScript is still loaded, so thats likely to be a pain.
Id note that Turbolinks is actually pretty large and complex, so it would be worth reading its own documentation rather than just the small snippets from Rails. https://github.com/turbolinks/turbolinks
这篇关于Rails 在渲染后强制重新加载 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!