【问题标题】:How do I use CoffeeScript in Rails?如何在 Rails 中使用 CoffeeScript?
【发布时间】:2011-08-01 18:28:41
【问题描述】:

在我的控制器的 create 操作中,我有以下内容:

def create
  @article = Article.find(params[:id])

  respond_to do |format|
    if @comment.save
      format.js { render 'success.js' }
    else
      format.js { render 'failed.js' }
    end
  end
end

在我的app/views/comments/failed.js.coffee 中,我有:

alert 'Write smth!' if $("#comments_error").length == 0

我收到以下错误:

ActionView::MissingTemplate (Missing template comments/failed,
  inherited_resources/base/failed, application/failed with
  {:locale=>[:en, :en],
   :formats=>[:js, :html],
   :handlers=>[:haml, :builder, :erb]})

我做错了什么?

【问题讨论】:

    标签: javascript ruby-on-rails templates coffeescript controller-action


    【解决方案1】:

    在撰写本文时,Rails 不支持使用咖啡脚本文件进行响应。 然而,这种情况将会改变。

    同时在你的 Gemfile 中添加:

    gem 'coffeebeans'
    

    然后将你的观点命名为 action.js.coffee

    作为额外的好处,文件将首先通过 erb,即使它没有在文件名中声明。

    【讨论】:

    • 我还有一个问题,如果我的 action.js.coffee 文件中有一些 ruby​​ 代码,我应该怎么做?我尝试将 action.js.coffee 重命名为 action.js.coffee.erb,但收到错误:ActionView::MissingTemplate
    猜你喜欢
    • 2013-06-14
    • 2012-05-05
    • 2011-08-31
    • 1970-01-01
    • 2018-03-05
    • 2023-03-08
    • 2019-05-02
    • 2020-09-22
    • 2012-12-04
    相关资源
    最近更新 更多