2

I am using Rails 3.1, it seems that I could not use Ruby script after a CoffeeScript statement in my create_error.js.coffee view file for the AJAX response.

If I do it like this it will work:

<% if @attached_image.errors.any? %>
  alert 'something is wrong'
<% end %>

but if it is like the following, with a <% %> after the CoffeeScript statement,

errors_block = '<div id="errors_block"></div>'
<% if @attached_image.errors.any? %>
  something..
<% end %>

I will alway get an exception of ActionView::Template::Error (Parse error on line 6: Unexpected 'INDENT') on the if line. I've tried several samples, everytime it happens when a ruby <% %> comes after a coffeescript statement.

Why is that?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
larryzhao
  • 3,173
  • 2
  • 40
  • 62

2 Answers2

6

Sounds like you're running into indentation issues in your post-ERB CoffeeScript. Given this:

errors_block = '<div id="errors_block"></div>'
<% if @attached_image.errors.any? %>
  something...
<% end %>

The output will look like this when the if condition is true:

errors_block = '<div id="errors_block"></div>'

  something...

and that indentation starts a new block that doesn't make sense in that context; hence the "Unexpected 'INDENT'" error from the CoffeeScript compiler. You can see this in action in this snippet on coffeescript.org.

CoffeeScript is very sensitive to indentation so mixing ERB and CoffeeScript like that isn't a good idea. You'd be better off putting @attached_image.errors into a CoffeeScript variable and then doing the logic in CoffeeScript, something more like this (untested code):

errors = <%= @attached_image.errors.to_a.to_json.html_safe %>
errors_block = '<div id="errors_block"></div>'
if errors.length > 0
  something...

The JSON version of the errors array should be valid CoffeeScript so errors will be a CoffeeScript array. The to_a is there in case errors returns nil, I'm not sure off the top of my head if errors.nil? is possible but a little extra paranoia never hurt anyone.

You could also do this:

errors_block = '<div id="errors_block"></div>'
<% if @attached_image.errors.any? %>
something..
<% end %>

but that's harder to read and you will forget. You're better off using ERB to generate CoffeeScript data and letting CoffeeScript handle the logic.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
0

Your file should be called:

create_error.js.coffee.erb

So you could add ruby on it.

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
  • I found this https://github.com/rails/coffee-rails/issues/13 yesterday which told me to use js.coffee(without the erb part), and it actually works quite fine with the ruby code part, is only the coffeescript processor won't pass if there's `<% %>` after one of the coffee statement. But it works the other way around, or in between. I will edit the question to make it more clear. – larryzhao Feb 02 '12 at 14:54