因此,我正在使用一些 javascript 响应程序来执行一些操作。所以,举例来说,
def create
#code omitted
respond_to do |format|
if @post.save
format.html { redirect_to discussion_posts_path(@post.discussion), notice: 'Post was successfully created.' }
format.js
else
format.html { render :action => "new" }
end
end
end
#create.js.erb
$("#discussion_posts_table").append("<%= escape_javascript(render(@post)) %>");
$("#post_body").val("");
现在假设我想将这些 javascript 函数转换为 Coffeescript(抛开它几乎没有任何收获的事实),如果我将文件更改为create.js.coffee.erb
它停止工作。在 UJS 中使用 CoffeeScript 的正确方法是什么?
作为记录,我有coffee-rails
在我的 Gemfile 中,并在资产管道中使用 Coffeescript。
我以前见过使用过 .coffee.erb 。仍在寻找该项目。
好吧,如果它们仍然在您的视野中,只需使用 .js.coffee
编辑 1:显然 Rails 仍然会奇怪地处理 erb。
编辑2:此外,您可以将 erb 添加到 js.coffee 文件的末尾,它们只是按特定顺序进行预处理,因此可能会导致 Rails 崩溃。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)