如何在 Rails 中使用 CoffeeScript?
在我的控制器的 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]})
什么是我做错了吗?
In my controller's create
action, I have the following:
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
In my app/views/comments/failed.js.coffee
, I have:
alert 'Write smth!' if $("#comments_error").length == 0
I receive the following error:
ActionView::MissingTemplate (Missing template comments/failed,
inherited_resources/base/failed, application/failed with
{:locale=>[:en, :en],
:formats=>[:js, :html],
:handlers=>[:haml, :builder, :erb]})
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在撰写本文时,Rails 不支持使用咖啡脚本文件进行响应。
然而,这种情况将会改变。
同时在您的 Gemfile 中添加:
然后将您的视图命名为 action.js.coffee
作为额外的好处,文件将首先通过 erb,即使它没有在文件名中声明。
At the time of this writing Rails does not support responding with a coffee-script file.
This however is going to change.
Meanwhile in your Gemfile add:
then name your views action.js.coffee
As added bonus the file will pass through erb first, even if it's not declared in the file name.