(rails3) passing ruby variables to javascript file - javascript

When rendering a javascript file (not RJS, just a regular JS file) from a rails controller, is there a way to pass variables to it, which can then be used by functions in the JS file being called? I've tried :locals => {} but that doesn't work.

If you're rendering inside a js file you should use rjs. If you really don't like rjs (understandable) then how about rendering your variables first, then including your script?
<% javascript_tag do %>
var my_var = <%= #my_var.to_json %>;
<% end %>
<% javascript_include_tag 'your-js-file-that-expects-my_var-to-exist.js' %>

Related

How to insert user_id to coffeescript file

I'm stacked. Have model User and Event and also fullcalendar.
$(document).ready ->
$("#calendar").fullCalendar(
events: '/events.json'
)
Here is my coffeescript file but it doesnt work because it needs to be events: '<%=#user.id%>/events.json'. But ruby-code can't be in coffeescript file, so I need to get user_id.
How should I do this?
It is possible when the html is being generated. One way is to put in the bottom of your file js code which assigns user's id to globally available configuration and then read this id from this variable.
for example in your erb
<%= javascript_tag do %>
window.Configuration = {};
window.Configuration.UserId = <%= #user.id %>
<% end %>
And later in your coffee file
events: "#{window.Configuration.UserId}/events.json"

Rails ERB Inside Javascript

I'm trying to use some View instance variables inside Javascript.
My JS file is included in the head tag of my HTML. And it's a *.js file in assets/javascript.
When I try to...
console.log(<%= #some_instance_variable %>)
... I get syntax error. And if I try to...
console.log('<%= #some_instance_variable %>')
... the output is just the string <%= #some_instance_variable %>. How can I do to obtain the instance variable's actual value?. I'd really appreciate any help!.
Regards!.
You can include it with:
<%= javascript_include_tag('file_name.js.erb'.sub('.erb', '')) %>
But in fact it's a very ugly solution. If you want to use rails variables in JS scripts, use gon gem.

javascript inside html-erb files issue

I ma trying to understand why this javascript inside my erb file is not working
<%= javascript_tag do %>
var tmp= "<%=j render :partial => "layouts/alert_error_message", :object=>#campaign %>";
<% end %>
I get this message
compile error
/var/www/gitorious/app/views/campaigns/new.html.erb:32: syntax error, unexpected ')'
...r.concat(( javascript_tag do ).to_s); #output_buffer.concat ...
/var/www/gitorious/app/views/campaigns/new.html.erb:38: syntax error, unexpected kENSURE, expecting ')'
When I use instead <% javascript_tag do %>, the render method is correctly processed but the tmp variable is not set in my javascript (console.log(tmp) returns undefined)
Edit : I wrapped the render method with escape_javascript and it worked but I still don't undestand why it is only working when I use <% javascript_tag do %>. Is it not suppose to work with <%= javascript_tag do %> ?

pass a parameter from a views to a javascript

I have the next variable in my main_controller.rb:
def create:
#token = "24vgd32"
end
in my views/create.erb, I wrote:
<%= javascript_tag do %>
window.my_token = '<%= j #token %>';
<% end %>
by this way, I can use this variable in any of the JavaScript files that this page references.
but how can I use it in my application.js?
this is my application.js:
$(document).ready(function(){
licensario.getLicense({
wizardToken: my_token
});
});
any help appreciated!
In your create.html.erb use this
<%= hidden_field_tag :my_token, #token, :id => 'some_field_id' %>
replace the some_field_id and my_token with desired names.
In your javascript file
$(document).ready(function(){
token = $('#some_field_id').val();
//use your token for any other means.
});
I see 3 ways :
You can use erb in your js file, naming it application.js.erb. However, the data you want to pass must be available when the file is compiled, so this is probably not what you want to do;
You can make a XHR request to your server to fetch the value you want;
You can embed the value in the dom of your view. Some will suggest using a hidden field, I rather tend to use data attributes. For instance, <body data-my-value="32">.... You can retrieve it in your js by doing $('body').data('my-value'). Be careful to call this once the dom is loaded.
I tend to use the latter.
window.my_token makes my_token a variable on the global scope; once it's been defined it is available to application.js as simply my_token.

Returning javascript from rails helper

I am having problems returning javascript from helper functions. I am unable to get the following test case to work:
function in helpers/application_helper.rb
def show_stuff
return '$("div#flash").html("<p> stuff </p>");'
end
now I try to call this helper function in general.js.erb
<%= show_stuff %>
Here is the output
$("div#flash").html("<p> stuff </p>");
I've also tried show_stuff.html_safe and raw show_stuff and had no luck. I feel the issue is Rails auto-escaping html but have been unable to find a solution.
ERB is escaping the characters in your outputted Javascript.
To prevent this happening use the raw method in your template.
<%= javascript_tag do %>
<%= raw(show_stuff) %>
<% end %>
I've added the javascript_tag method around this for context.

Categories