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.