0

i'm trying to pass a javascript variable into the ruby on rails 「 url_for」method

is that possible??

<a href="<%= url_for(:controller => 'epgs', :action => 'show', :parameter1 => myJavaScriptVariable %>" data-inline="true">

1 Answer 1

2

No, JavaScript variables are not in the scope when your ERB is being rendered as this is being done server-side. Javascript can only be used once the page has been fully rendered by Rails and returned to the user (i.e in the user's browser). If you really need a JavaScript variable in the URL then a simple way to do it would be to add the extra parameter to the href using JavaScript once the page has loaded:

  new_link = $('#link').attr('href') + '&parameter_1=' + myJavaScriptVariable;
  $('#link').attr('href', new_link);

using jQuery or something similar.

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

1 Comment

Quite right! Obviously, the #link element won't be available to replace the href until the DOM is fully loaded. Didn't want to make the response too JS or jQuery heavy, though. :)

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.