0

How can I test a ruby variable in coffeescript? I'm updating a rails legacy application and trying to keep the closes of the original format I can in the first step. With .js.erb, I could simply do <%= if @var %> to achieve what I need.

Bellow is the code of the view with two wrong tries:

if @review_change_needs_refresh # Don't work
# if "#{@review_change_needs_refresh}" == "true" Don't work too
  location.reload()
else
  button_div_id = "#ManagedWorkFor#{@work.class.to_s}#{@work.id}"
  label_div_id = "#chat_#{@managed_work.to_language_id}_label"

  $(button_div_id,"#{escape_javascript(render(:partial => "/managed_works/managed_work", :object=>@work))}")

  if @checkbox_label.any?
    for element in $("##{label_div_id}") then do => $("##{label_div_id}", "#{@checkbox_label}")

On the action, the variable that is being tested is a boolean:

@review_change_needs_refresh = params[:review_change_needs_refresh] == 1

I get the following error on the first line of the coffeescript file:

ActionView::Template::Error (SyntaxError: unexpected {):

So, How can I test a ruby varaible in coffeescript?

fotanus
  • 19,618
  • 13
  • 77
  • 111
  • Maybe this can help http://stackoverflow.com/questions/8108511/how-to-access-instance-variables-in-coffeescript-engine-inside-a-slim-template – MrYoshiji May 20 '13 at 14:22

1 Answers1

1

If you really want Ruby code in your CoffeeScript file, just rename it to *.js.coffee.erb. Then you can just put the <%= %> tags in the file.

Like the the question linked by MyYoshiji says, the Rails team decided not to support what you are trying to do for performance reasons.

I personally would set some JS vars in the view file using my Ruby data, and then load those vars in my CoffeeScript file.

Nick Messick
  • 3,202
  • 3
  • 30
  • 41
  • Thank you, that is what I was looking for. I understand that this is not the best solution but, as I said, I'm upgrading an legacy application with tons of pages, so changing the logic of everything at once might be troublesome. – fotanus May 20 '13 at 16:40