3

在我的控制器的create行动,我有以下几点:如何在Rails中使用CoffeeScript?

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]}) 

我是什么做错了?

回答

7

在撰写本文时,Rails不支持使用coffee-script文件进行响应。 但是这将会改变。

与此同时,在你的Gemfile中添加:

gem 'coffeebeans' 

然后命名你的意见action.js.coffee

作为额外的奖励文件将通过该局第一,即使它不是在文件名中声明。

+0

非常感谢! – zolter

+0

我还有一个问题,如果我的action.js.coffee文件中有一些ruby代码,我该怎么办?我尝试将action.js.coffee重命名为action.js.coffee.erb,但我收到错误:ActionView :: MissingTemplate – zolter

+0

奇怪,但问题本身解决 – zolter