1

I have a js file where I want to pass some Rails variable to such as url. What I do is:

#main.haml.html

<script type="text/javascript">
  var myUrl = <%=some_url%>;
</scirpt>

#script1.js
$.ajax({
  method: 'POST',
  url: myUrl,
  success: function(data) {
    //..........
  },
  error: function(a, b, c) {
        //..........
  }
});

I think it seems the proper way of doing this. Is this the idiomatic way of doing it? Is there a better way?

1
  • A better place for this questions would be codereview: codereview.stackexchange.com. But I will offer this: There's nothing specifically wrong with what you are doing - as this ajax method was created for that purpose. However, you want to think about proper code organization. Will you have a lot of ajax calls? Is your javascript file growing in size? You don't want to have scattered ajax calls all over the place within an increasingly complex javascript file. Commented Mar 30, 2014 at 1:36

1 Answer 1

1

There's an episode on Rails Casts about this topic here

generally you can put execute scripts to put variable under window object or create a html div to just to hold data. Since these are more static to the browser environment

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

1 Comment

isn't that the same thing as what I do?

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.