简体   繁体   中英

How do I use CoffeeScript in Rails?

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?

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:

gem 'coffeebeans'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM