Rails 3 and jQuery- how to transmit ruby variable to javascript - javascript

I would like to ask you - I have javascript code and I need into this part of code transmit variable with data from database. This variable is in controller. How can I do it?
I tried something like (in *.js):
$("#div").append('<%= escape_javascript(#test) %>hhhhhh');
or
$("#div").append('<%= #test %>hhhhhh');
But the codes above not works... Could you help me, please, how to do?
Thank you, Manny

If you are rendering a response to AJAX-call, that code should go in your_action.js.erb file. Otherwise, if it's just inline JavaScript, place it in your_action.html.erb under app/views/your_controller directory.
Correct me if I understood you wrong.

Related

parsing different javascript from same source

i know these things can be done easily through php but in shopify store i saw javascript attached like this async.
https:\/\/sometime.com\/shopify\/script.js?shop=storename1.myshopify.com
https:\/\/sometime.com\/shopify\/script.js?shop=storename2.myshopify.com
https:\/\/sometime.com\/shopify\/script.js?shop=storename3.myshopify.com
and to my surprize all three code was different.
anyone can tell what is happening behind the scene. how it is possible that different javascript is passes from same script.js ??
any help will be great learning
Here
https:\/\/sometime.com\/shopify\/script.js
is essentially not like there is a file script.js inside directory /shopify, rather it is a route. And then the shop acts here as a query parameter for GET request, the server reads it and returns the response accordingly.

How to access environment variable from rails on your javascript?

I am trying to set up a key system in the application I'm building. I have a code that has this logic:
$('.key-test-button').on('click', function(){
if ($('.key-test-input').val() === "MYKEYHERE"){
$('#hidden-div-instructor').show();
}
});
And My Idea was to replace "MYKEYHERE" for a hidden value on my code that I would store as a environment variable. I know how to do that for APIs for example, inside rails, but Im not familiar how I could access this variable inside my javascript/jquery script. I would love if someone could give me a insight. Thanks you.
As #davidhoelzer mentioned, you can't do this with pure js (as far as I know)... you could, however, change the .js file to .js.erb and use ruby to access/manipulate your environment variable that way.

How can I read a value from a webpage?

I ask now apologize for the issue that I will do, but already searched everywhere and found nothing relevant.
I need to read a data from a website, but not even managed to do.
The information is as follows:
And I need to read the numbers.
Did I could help please?
Thank you all.
If you need I clarify my doubts.
Very vague and unclear but it looks like json data so... how is this data retrieved? Is this a simple webpage loaded in a browser or the result from some ajax / rest request?
You could try using DOMDocument and then searching the DOM for the PRE tag and getting the contents that way.
If it is a json response from an ajax call then it is a trivial task. More info is required to get a sensible answer

rails code within javascript

I am trying to use some rails code withing a javascript and need to have that rails code be dynamically changed. Here's the line of code:
$(this).replaceWith("<%= escape_javascript(render(:partial => 'shared/products')) %>");
The 'shared/products' is the part I want to change based off information passed earlier in the javascript. How do I insert a value from javascript so that instead of 'shared/products' the products portion can be a variable? Hope this makes sense. I'm not the most experienced jQuery/javascript programmer, so any help is very much appreciated. Thanks in advance!
No, you cannot do that way. rails codes may run on page loads, but when you write those with javascript, those are just texts. If you really need to run server side scripts, use AJAX.

Localization Helper, How to retrieve values from javascript?

I used localization helper from Matt Hawley. It's really working great. I have a problem though in getting the values on javascripts/jquery.
I can't retrieve the resources text using this:
example: alert('<%=Html.Resource(\"Strings,SomeKey\")%>');
Any help is greatly appreciated.
Best
Thank you BalusC for the reply. Greatly appreciated. I already got a work around. I declared variable on my .aspx page with the resources key values. Then call the variable on my separate js. So I have it like this:
In my aspx page.
<script>
var foo="<%=Html.Resource("Strings,SomeKey")%>";
</script>
In my js. I just simply called the variable foo.
alert(foo);
There I have it!:)
I got it wrong for the first time since my variable was declared on my external js. Therefore asp.net will not be able to interpret it. Instead it will be rendered as plain text.
Best regards

Categories

Resources