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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.