0

I know this sounds insane. ;)

My Line :

"#{escape_javascript(content_tag(:a, "edit", :href => edit_object_payment_url(@object, "\#{payment_id}")))

Then with interpolation, I inject my variables. Payment_id is a javascript variable. Is it possible to syntactically pass that variable into this escaped_javascript statement?

1 Answer 1

1

Your Rails view won't have access to JavaScript variables, so you won't be able to call the Rails URL helper methods like edit_object_payment_url using JavaScript variables. But you could use JavaScript to construct the URL on the client side.

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

4 Comments

Is it then possible to perform a ternary expression within a javascript string interpolation?
Not sure about within interpolation, but you can do "here's " + (x ? 'one' : 'a') + " string"
I don't believe that's true. My code : "<a href='objects/357/payments/"+ typeof payment_id === 'undefined' ? '357' : #{payment_id} + "'>Delete</a>" Still returns undefined variable payment_id
That's because, within an <a> element you're not executing JavaScript. There you are expected to be writing valid HTML, which doesn't support concatenating values with +, or interpolation for that matter. What you can do, though, is (within a <script>), run some JS code like this (using jQuery): $('a#my_link').prop('href', 'objects/357/payments/' + payment_id);

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.