【问题标题】:JS template not rendered by Ruby on Rails on HerokuHeroku 上的 Ruby on Rails 未呈现 JS 模板
【发布时间】:2013-02-01 20:55:27
【问题描述】:

我将我的应用部署到 Heroku 上的暂存应用。它在本地工作 100% 正常。

http://exposehair-staging.herokuapp.com

我有两个渲染 JS 的控制器。当我提交一个远程表单时,它会调用预期的操作并在其中执行代码......除了它没有呈现 JS 模板。正在对操作中的数据库进行更改,但这就是应用程序停止的地方。这发生在渲染 JS 的两个控制器上。请记住,该应用在本地开发模式下可以正常运行。

我想知道问题是否在于我的应用程序配置与 Heroku 一起使用?

这是我的 Heroku 应用程序日志:

2013-02-01T20:17:17+00:00 app[web.1]: Started GET "/" for 68.230.151.152 at 2013-02-01 20:17:17 +0000
2013-02-01T20:17:17+00:00 app[web.1]:   Rendered blurbs/_show.html.slim (2.3ms)
2013-02-01T20:17:12+00:00 app[web.1]: DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/config/environment.rb:10)
2013-02-01T20:17:12+00:00 app[web.1]: DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/config/environment.rb:10)
2013-02-01T20:17:08+00:00 heroku[web.1]: Starting process with command `bundle exec thin start -R config.ru -e staging -p 23436`
2013-02-01T20:17:15+00:00 app[web.1]: >> Thin web server (v1.5.0 codename Knife)
2013-02-01T20:17:15+00:00 app[web.1]: >> Maximum connections set to 1024
2013-02-01T20:17:15+00:00 app[web.1]: >> Listening on 0.0.0.0:23436, CTRL+C to stop
2013-02-01T20:17:17+00:00 app[web.1]: Completed 200 OK in 232ms (Views: 114.2ms | ActiveRecord: 63.0ms)
2013-02-01T20:17:17+00:00 app[web.1]: Processing by PagesController#index as HTML
2013-02-01T20:17:17+00:00 app[web.1]:   Rendered business_hours/_index.html.slim (24.9ms)
2013-02-01T20:17:17+00:00 app[web.1]:   Rendered pages/index.html.slim within layouts/application (78.4ms)
2013-02-01T20:17:18+00:00 heroku[router]: at=info method=GET path=/assets/foundation/orbit/left-arrow-5a8ccc07cfa3eae4292d7c705fc7b0a2.png host=exposehair-staging.herokuapp.com fwd=68.230.151.152 dyno=web.1 queue=0 wait=5ms connect=18ms service=32ms status=200 bytes=522
2013-02-01T20:17:18+00:00 heroku[router]: at=info method=GET path=/assets/foundation/orbit/right-arrow-6fca0c667c001b2352c44c9fb1e55528.png host=exposehair-staging.herokuapp.com fwd=68.230.151.152 dyno=web.1 queue=0 wait=2ms connect=7ms service=7ms status=200 bytes=3242
2013-02-01T20:31:22+00:00 heroku[router]: at=info method=GET path=/assets/application-6cd756f9981b294d6c6bd575ae87f94b.css host=exposehair-staging.herokuapp.com fwd=68.230.151.152 dyno=web.1 queue=0 wait=0ms connect=59ms service=15ms status=304 bytes=0
2013-02-01T20:31:22+00:00 heroku[router]: at=info method=GET path=/assets/orbit_05-c6ea9ca2a7468ee90a6c8bfc87eb607e.jpg host=exposehair-staging.herokuapp.com fwd=68.230.151.152 dyno=web.1 queue=0 wait=0ms connect=0ms service=1ms status=304 bytes=0
2013-02-01T20:31:22+00:00 heroku[router]: at=info method=GET path=/assets/orbit_03-42632d5ec29efd0b5e4c8cae7336e47c.jpg host=exposehair-staging.herokuapp.com fwd=68.230.151.152 dyno=web.1 queue=0 wait=0ms connect=0ms service=1ms status=304 bytes=0
2013-02-01T20:31:22+00:00 heroku[router]: at=info method=GET path=/assets/orbit_02-64cae3654d21289ce3fe325715c395ed.jpg host=exposehair-staging.herokuapp.com fwd=68.230.151.152 dyno=web.1 queue=0 wait=0ms connect=2ms service=3ms status=304 bytes=0
2013-02-01T20:31:29+00:00 heroku[router]: at=info method=POST path=/blurbs/1 host=exposehair-staging.herokuapp.com fwd=68.230.151.152 dyno=web.1 queue=0 wait=0ms connect=7ms service=167ms status=500 bytes=643
2013-02-01T20:31:22+00:00 heroku[router]: at=info method=GET path=/assets/application-ca9f839e91c53f606b8c9344a4a1243e.js host=exposehair-staging.herokuapp.com fwd=68.230.151.152 dyno=web.1 queue=0 wait=0ms connect=0ms service=1ms status=304 bytes=0
2013-02-01T20:31:22+00:00 heroku[router]: at=info method=GET path=/ host=exposehair-staging.herokuapp.com fwd=68.230.151.152 dyno=web.1 queue=0 wait=8ms connect=16ms service=148ms status=200 bytes=30683

还有我的 gemfile

source 'https://rubygems.org'
ruby '1.9.3'

# Gems required by all environments
gem 'jquery-rails'
gem 'rails', '3.2.9'

gem 'acts-as-taggable-on'                     # https://github.com/mbleigh/acts-as-taggable-on
gem 'carrierwave'                             # https://github.com/jnicklas/carrierwave
gem 'combined_time_select', '~> 1.0.0'        # https://github.com/excid3/combined_time_select
gem 'fog', '~> 1.3.1'                         # https://github.com/fog/fog
gem 'font_awesome', '~> 3.0'                                # https://github.com/balexand/font_awesome
gem 'foundation_rails_helper',                  # https://github.com/sgruhier/foundation_rails_helper
  :git => 'git://github.com/sgruhier/foundation_rails_helper.git',
  :branch => 'master'
gem 'mini_magick'                             # https://github.com/probablycorey/mini_magick
gem 'rack-canonical-host'                     # https://github.com/tylerhunt/rack-canonical-host
gem 'redcarpet'                               # https://github.com/vmg/redcarpet
gem 'roboto'                                  # https://github.com/LaunchWare/roboto
gem 'slim-rails'                              # https://github.com/leogalmeida/slim-rails
gem 'sorcery'                                 # https://github.com/NoamB/sorcery
gem 'stamp'                                   # https://github.com/jeremyw/stamp
gem 'validates_email_format_of'               # https://github.com/alexdunae/validates_email_format_of

# Gems requred only for development
group :development, :test do
  gem 'better_errors'                         # https://github.com/charliesome/better_errors
  gem 'binding_of_caller'                     # optional dependancy of better_errors
  gem 'sqlite3'
end

# Gems required for heroku
group :staging, :production do
  gem 'foreman'
  gem 'pg'
  gem 'thin'
end

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'coffee-rails', '~> 3.2.1'
  gem 'sass-rails',   '~> 3.2.3'
  gem 'uglifier', '>= 1.0.3'

  gem 'compass-rails'                                         # https://github.com/Compass/compass-rails
  gem 'zurb-foundation'                                   # http://foundation.zurb.com/docs/
end

我的 application.rb 文件中也有以下设置:

config.assets.initialize_on_precompile = false

更新:这里是一些要求的相关代码。 Heroku 上没有呈现任何 js 模板,但它们完全可以在本地工作。这就是我一开始省略它的原因。

一个例子:

控制器/blurbs.rb

class BlurbsController < ApplicationController
  before_filter :require_login

  # PUT /blurbs/1
  def update
    @blurb = Blurb.find(params[:id])

    respond_to do |format|
      if @blurb.update_attributes(params[:blurb])
        format.js { }
      else
        format.js { render 'reload' }
      end
    end
  end
end

views/blurbs/reload.js

# Reload form
$("#editBlurb<%= @blurb.id %> form").html("<%= escape_javascript(render :partial => 'form', :locals => {:blurb => @blurb}) %>")

views/blurbs/_form.html.slim

= form_for blurb, remote: true do |f|
  .field
    = f.text_area :content, :label => blurb.name.titleize
  .actions = f.submit 'Save'

【问题讨论】:

  • 一些相关代码怎么样? (您的控制器操作、Javascript 模板、模板的路径等...)
  • 我不认为这些信息是必要的,因为它在本地工作......但无论如何我都会添加它。感谢您查看此内容。
  • 我提供 10 美元通过贝宝支付给第一个可以告诉我如何修复我的应用程序的人。视频录制在这里:vimeo.com/59312693
  • 我看到 JS 模板在您的日志中抛出 500:“... method=POST path=/blurbs/1 ... status=500 ...”。尝试在生产环境中调高日志级别,看看是否可以从中获得更多有用的数据。
  • 此外,您可以通过使您的开发环境和生产环境尽可能相似来消除错误来源。正如多米尼克所展示的,POST 失败了——它做了一堆数据库的东西(以及渲染 JS)。例如,我看到您正在使用 sqlite 进行开发。强烈建议您在本地使用完全相同的数据库 - 数据库之间存在细微差别,即使您认为您只是使用标准 SQL。最后,由于some security considerations,请考虑使用 Rails 3.2.11。

标签: javascript ruby-on-rails heroku render


【解决方案1】:

我把日志级别转为debug

# staging.rb
config.log_level = :debug

并注意到以下内容:

Started PUT "/blurbs/1" for 68.230.151.152 at 2013-02-11 00:26:26 +0000
2013-02-11T00:26:26+00:00 app[web.1]: Processing by BlurbsController#update as JS
2013-02-11T00:26:26+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"rXyxUFU5qhjR4YieynLVAnr5ADnaUXn+No4Qruw5CFo=", "blurb"=>{"content"=>"# About us\r\n\r\nTell **your story** here."}, "commit"=>"Save", "id"=>"1"}
2013-02-11T00:26:26+00:00 app[web.1]: Completed 500 Internal Server Error in 318ms
2013-02-11T00:26:26+00:00 app[web.1]: 
2013-02-11T00:26:26+00:00 app[web.1]: ActionView::MissingTemplate (Missing template blurbs/update, application/update with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :slim]}. Searched in:
2013-02-11T00:26:26+00:00 app[web.1]:   * "/app/app/views"
2013-02-11T00:26:26+00:00 app[web.1]:   * "/app/vendor/bundle/ruby/1.9.1/gems/roboto-0.1.0/app/views"
2013-02-11T00:26:26+00:00 app[web.1]: ):

Heroku 上的 Rails 只寻找 js、html、erb、builder 或 slim。不是咖啡脚本。然后我记得咖啡脚本在我的 gemfile 的资产组中。它在 heroku 上进行了预处理,但是 gem 不能与应用程序一起运行。所以我将 gem 移到资产组之外,我的应用现在可以在 Heroku 上完美运行!

由于@Dominic 的回答将我带到了那里,如果您想索取我为解决我的问题而提供的 10 美元,请通过您的贝宝联系我。对此,我真的非常感激。谢谢。

https://stackoverflow.com/users/1704688/keil-miller

【讨论】:

  • 哈哈 - 无需支付宝。很高兴帮助某人解决他们的问题:)
  • 有趣的是,您在问题中将 JS 模板命名为“views/blurbs/reload.js”。该文件实际上命名为“views/blurbs/reload.js.coffee”吗?
  • 谢谢。是的,它实际上是 .js.coffee。我想我在标题中忘记了这一点。但是,代码确实是coffeescript。没有分号! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 2019-04-01
相关资源
最近更新 更多