Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 519
zolter Avatar asked Jun 03 '26 02:06

zolter


1 Answers

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.

like image 54
kain Avatar answered Jun 05 '26 15:06

kain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!