gpt4 book ai didi

除非使用 turbolinks 和 rails 4 重新加载页面,否则不会执行 javascript

转载 作者:行者123 更新时间:2023-11-29 15:38:55 25 4
gpt4 key购买 nike

我关注了 railscast tutorial , 但我发现我无法加载“已保存的 token ”。

我认为问题是当点击“编辑”链接(由脚手架创建)时,

它没有触发预加载功能,你如何解决 Rails 4 中的问题

enter image description here查看页面

  .form-inputs
= f.input :name
= f.input :content
= f.text_field :user_tokens, data: {load: @article.users}

我的 CoffeeScript

$(document).ready -> 
$('#article_user_tokens').tokenInput '/users.json',
theme: 'facebook',
prePopulate: $('#article_user_tokens').data('load')

需要css和js文件

 *= require token-input-facebook
//= require jquery.tokeninput

我通过这个解决方案修复了错误,使用ready page:load

但我不明白,为什么会这样

$(document).on "ready page:load", ->
$('#article_user_tokens').tokenInput '/users.json',
theme: 'facebook',
prePopulate: $('#article_user_tokens').data('load')

最佳答案

启用 Turbolinks 后,Rails 使用 Ajax 仅加载已更改的页面元素,而不是加载整个新页面。因此,从 jQuery $(document).ready 的 Angular 来看,通常没有页面加载。参见 here有关在 Rails 4 中使用 .js 的精彩帖子。

$(document).on 'ready, page:change' 在未完全重新加载的页面上经常用作 $(document).ready 的替代品因为 Rails 4 中的 Turbolinks。我认为如果你切换到它,你的问题应该会消失。

您的语法也可能与 $(document).on "ready page:load" 有点不一样。已有other reports readypage 之间缺少逗号时出现的问题。

关于除非使用 turbolinks 和 rails 4 重新加载页面,否则不会执行 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23212898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com