3

Is there a way to implement if statements inside :javascript filter with HAML in Rails?

I've tried various ways such as

:javascript
  $(function(){
  - if @booth.greeting_video?
    = $('#greeting_video').modal();

But they do not seem to be working at all.

Is there a clean way of implementing this?

1 Answer 1

2

The filters in HAML are processed separately from the other code, and the only thing allowed here is the #{} method, which just inserts a Ruby value.

Theoretically you can insert your condition there, and return different values depending on it. And I can't guess what you're trying to do with = $('#greeting_video').modal(); - it looks like javascript, but why is there = sign before, making it look like ruby insertion?

And, it also needs to be said, it's not really a good idea to mix up back-end and fron-end so much. The js variable can be set here, and somewhere in another file the modal would be rendered or not, depending on that variable (just another way suggestion)

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

2 Comments

Thanks. How would you implement if condition for javascript lines then? Surely you can't insert external javascript link for each conditions containing one or two lines of javascripts.
the thing I was thinking about is just put #{'$('#greeting_video').modal();' if @booth.greeting_video?} as it's plain text there

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.