Rails: rendering js.erb via an AJAX call

From JavaScript, I call the controller via AJAX, for example:

$.ajax({ type: 'GET', url: '/books' } 

In my controller, I:

 def index render 'lightbox.js.erb' end 

On my routes, I:

 resources :books do member do get :lightbox end end 

In lighbox.js.erb, I have:

 alert("Hello world!"); 

For some reason, a warning will never be triggered. I do not receive error messages either through the server or through Firebug. I am at a loss what could go wrong. Any ideas?

+8
ajax ruby-on-rails
source share
7 answers

It turns out that on the client side, my JavaScript was being processed as text. I confirmed this by watching the console channel. He said:

 Started GET "/books/lightbox?book=4&username=tbaron&_=1344009191129" for 127.0.0.1 at 2012-08-03 10:53:11 -0500 Processing by BooksController#lightbox as text 

These last two words should have read "like JS." After rooting, I found this blog post , which led to a surprisingly simple solution. Add "dataType: script" to the AJAX call:

 $.ajax({ type: 'GET', url: '/books' dataType : 'script' } 

Thanks for all your help, everyone!

+20
source share

I think because you need to call books.js :

 $.ajax({ type: 'GET', url: '/books.js', success: function(data) { eval(data); } } 

In index action:

 def index respond_to do |format| format.js { render 'lightbox'} end end 

And lightbox.js.erb should be in the app / views / books

If it still does not work, try calling books/index.js You can also use firebug / chrome to verify that the server is responding to your ajax call.

+7
source share

You can try this script to call your action in java script

 var post_params = {}; var action = '/books/script_action'; $.post(action, post_params).success(function (data) { eval(data); }).error(function (data) { alert("Erro to call a action controller"); }); 

And in your controller try this. I suggest creating a new action

 def script_action render 'lightbox.js' end 

In the routes file:

 match "books/script_action/" => "books#script_action" 
+1
source share

Try adding this to the controller:

 respond_to :js 
0
source share

In your index action, rendering without layout:

 def index respond_to do |format| format.js { render 'lightbox', layout: false} end 

this is a job for me.

0
source share

For anyone who stumbles upon this and just wants to trigger a controller action that responds with the js.erb file, I accomplished it this way (with the help of Anthony Alberto Amazing answer ).

Controller action

 def some_action .. respond_to do |format| format.js { render 'some_action' } end end 

some_action.js.erb

 ... $('#someActionWrap').html('<%= j render "some/displaypartials/action_partial" %>') ... 

Ajax call

 var id = '<%= @someresource.id %>'; $.get('/someresource/' + id + '/action/', null, function(data){ eval(data) }, 'script'); 
0
source share

I have a similar problem with the link_to helper, but this has been fixed with remote:true

0
source share

All Articles