Rails 4:咖啡脚本仅在有发布请求或重新加载页面时有效

Rails 4: coffeescript only works when there is a post request or when i reload the page

本文关键字:新加载 加载 有效 请求 脚本 咖啡 布请求 Rails      更新时间:2023-09-26

我主要是一个后端开发人员,对JavaScript相关的东西(以及该地方存在的所有框架星系)并不那么熟练。我知道这很糟糕,但事实就是如此。

我对我遇到的问题变得疯狂,我可能错过了一些非常基本的东西。 我做了一些研究(谷歌+堆栈溢出圣经),没有发现任何与我遇到的问题类似的案例。 我想我只是不知道我在做什么。 让我解释一下。

发生了什么事情

我正在使用Rails 4进行一个小(无用的)项目,我尝试在coffeescript文件中编写一些javascript"代码"。

显然,我编写的 CoffeeScript "代码"仅在我重新加载页面或POST请求后(例如,当您提交表单时)时才有效。对于GET请求,例如在页面之间导航期间,没有任何反应。

我尝试了一个非常简单的基本基本 101 测试:在页面上显示一条警报消息(你好!)(它首先包含一个日期选择器)。

假设我有一个Article模型和一个用于编辑/创建/删除数据的关联ArticlesController。我有以下文件:

app/assets/javascripts/application.js
app/assets/javascripts/articles.js.coffeee
app/controllers/articles_controller.rb 
app/models/article.rb
app/views/articles/new.html.erb 
app/views/articles/index.html.erb 
etc.

我只是在app/assets/javascripts/articles.js.coffee中添加一行:

alert "Hello!"

如果我提交表单或重新加载页面,则会看到警报"您好!如果我只是点击一个链接,例如编辑一篇文章,什么都不会发生。

日志

当我查看这两种情况下的 rails 日志时,我有两种不同的情况

在"它不起作用"的情况下,当我执行简单的get请求(通过单击编辑链接)时,我在日志中都有这个:

Started GET "/articles/1/edit" for 127.0.0.1 at 2013-09-17 14:35:28 -0400
Processing by ArticlesController#edit as HTML
  Parameters: {"id"=>"1"}
  Author Load (0.3ms)  SELECT "authors".* FROM "authors" WHERE "authors"."remember_token" = 'ab6fa1c5257585de99459c60f24121c' LIMIT 1
  Article Load (0.2ms)  SELECT "articles".* FROM "articles" WHERE "articles"."author_id" = ? AND "articles"."id" = 1 ORDER BY published_at DESC LIMIT 1  [["author_id", 1]]
  Rendered shared/_error_messages.html.erb (0.1ms)
  Rendered articles/edit.html.erb within layouts/application (6.6ms)
  Rendered layouts/_shim.html.erb (0.1ms)
  Rendered layouts/_header.html.erb (0.6ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 44ms (Views: 40.4ms | ActiveRecord: 0.4ms)

在"它确实有效"的情况下,例如,当我重新加载相同的编辑页面时,它会重新加载所有内容。

Started GET "/articles/1/edit" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Processing by ArticlesController#edit as HTML
  Parameters: {"id"=>"1"}
  Author Load (0.3ms)  SELECT "authors".* FROM "authors" WHERE "authors"."remember_token" = 'ab6fa1c5257585de99459c60f24121c' LIMIT 1
  Article Load (0.1ms)  SELECT "articles".* FROM "articles" WHERE "articles"."author_id" = ? AND "articles"."id" = 1 ORDER BY published_at DESC LIMIT 1  [["author_id", 1]]
  Rendered shared/_error_messages.html.erb (0.1ms)
  Rendered articles/edit.html.erb within layouts/application (5.0ms)
  Rendered layouts/_shim.html.erb (0.1ms)
  Rendered layouts/_header.html.erb (0.4ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 26ms (Views: 23.4ms | ActiveRecord: 0.4ms)

Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Started GET "/assets/articles.css?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Started GET "/assets/articles.js?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
(i removed some of the lines to make it clearer)

我尝试了什么

我尝试使用

rake assets:precompile

但是一样的。

我尝试安装一些宝石,例如红宝石,咖啡师,它是一样的。

我错过了一些非常基本的东西吗?我应该读一本关于javascript基础的书吗?我应该因为在阿拉斯加不理解一些很容易的事情而被驱逐出境吗?我应该退休还是换工作?

感谢您的帮助。我陷入了抑郁。

这是因为涡轮链接宝石。当您单击链接时,浏览器不会真正重新加载页面,它只是执行 AJAX 请求并更改标题和正文,而其他所有内容保持不变。如果你想绕过它,你可以从application.js文件中删除turbolinks,或者安装jquery-turbolinks gem并在需要jquery或jquery ui之后立即放置//= require jquery.turbolinks

我还建议在你的咖啡中使用$ ->$(document).ready ->(在大多数情况下几乎是一样的)。

这是一个由于turbolinks的问题:你的javascript文件只被评估一次(在页面加载时)。

若要强制在页面更改时重新计算代码,请在 page:load 事件上添加事件侦听器:

ready = ->
  alert 'hello'
$(document).ready(ready)
$(document).on('page:load', ready)