zolter
zolter

Reputation: 7160

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?

Upvotes: 3

Views: 1044

Answers (1)

kain
kain

Reputation: 5570

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.

Upvotes: 7

Related Questions