Rails,CoffeeScript和ERB [英] Rails, CoffeeScript and ERB
问题描述
所以我正在搞几个javascript回应者几个动作。例如,
So I'm messing around witha few javascript responders for a few actions. So, for instance,
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的正确方法是什么?
Now lets say I wanted to convert these javascript functions to Coffeescript (setting aside the fact that it gains almost nothing), if I change the file to create.js.coffee.erb
it stops working. What's the proper way to use coffeescript in UJS?
为了记录,我在我的Gemfile中有 coffee-rails
,并在资产管道中使用coffeescript。
For the record, I have coffee-rails
in my Gemfile, and am using coffeescript in the asset pipeline.
推荐答案
我以前看过.coffee.erb。仍在寻找该项目。
I've seen .coffee.erb used before. Still looking around for that project.
确定如果他们仍在您的意见,只需使用.js.coffee
Ok if they're still in your views, just use .js.coffee
编辑1:显然Rails仍然会处理erb奇怪。
编辑2:此外,你可以添加erb到js.coffee文件的末尾,他们是jsut按照特定的顺序预处理,因此可能已经抛出Rails。
Edit 1: Apparently Rails will still process the erb oddly enough. Edit 2: Also, you can add erb to the end of a js.coffee file, they're jsut preprocessed in a particular order thus that might have thrown off Rails.
这篇关于Rails,CoffeeScript和ERB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!