【问题标题】:Rails javascript is included but not executing on heroku包含 Rails javascript 但不在 heroku 上执行
【发布时间】:2023-03-26 04:59:01
【问题描述】:

当我在本地运行应用程序时,一切正常。我的应用中只有 3 个 javascript 文件:

app/assets/javascripts/application.js

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
//= require_tree .

app/assets/javascripts/social_posts.js

//= require moment/moment 

时刻位于vendor/assets/javascripts

app/assets/javascripts/social_posts_actions.js.coffee

console.log 'hello'
# Format post dates
format_post_dates = ->
  $('time.moment').each ->
    dt = moment($(this).attr('datetime'))
    dtstr = dt.format('h:mm A')
    if dt.dayOfYear() != moment().dayOfYear()
      dtstr = dt.format('MMM Do, ') + dtstr
    $(this).html dtstr
    return
  return

add_edit_click_listener = ->
  $('.edit-social-post-action').click ->
    $('#socialPostMessage').val $(this).data 'message'
    $('#socialPostImageUrl').val $(this).data 'image-url'
    $('#myModal').modal()
    return
  return

# Add Handlers for the edit action
$(document).ready ->
  console.log 'hello doc.ready'
  add_edit_click_listener()
  return

当我在 heroku 上查看源代码时,它显示我的 cmets 和 console.log 调用正在 JavaScript 源代码中更新,但没有执行任何操作。

此外,如果我尝试从控制台调用 moment()(包含的库之一),则该函数不可用。

在推送到 heroku 之前,我尝试在本地预编译我的资产,但这并没有解决问题。

这是rails 4

【问题讨论】:

  • social_posts.js 在供应商文件夹中?而 moment 是 social_posts.js 中的一个函数?我正确吗?
  • @Marv-C no ... social_posts.js 在 assets/javascripts 下,moment/moment 在 vendor/assets/javascripts 文件夹中

标签: javascript ruby-on-rails ruby-on-rails-4 heroku


【解决方案1】:

您在运行git push heroku master 之前是否运行过rake assets:precompile RAILS_ENV=production?您需要在推送之前编译您的资产管道(图像、css 和 js 文件),否则它们不会上传到生产应用程序。

【讨论】:

  • 我在原始问题中说过,我在本地预编译它,然后再推送测试它。
【解决方案2】:

想出了解决办法。显然,因为我没有包括系绳,所以引导程序失败了。很烦:(

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 2011-10-29
    • 2013-09-27
    • 2014-04-28
    相关资源
    最近更新 更多