在我的 Controller 的 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]})
我做错了什么?
最佳答案
在撰写本文时,Rails 不支持使用 CoffeeScript 文件进行响应。 然而,这将会改变。
同时在您的 Gemfile 中添加:
gem 'coffeebeans'
然后将你的 View 命名为 action.js.coffee
作为额外的好处,文件将首先通过 erb,即使它没有在文件名中声明。
关于javascript - 如何在 Rails 中使用 CoffeeScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6902681/