Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot render JavaScript from Rails controller

I want to display a modal window with an error message, when the user has entered something invalid in a form, but render another action if everything is OK. However, when I try to display the modal window with

render :js => "jQuery.facebox(#{...})"

only the actual JavaScript called is displayed:

try {
  jQuery.facebox(...)
} catch (e) { alert('RJS error:\n\n' + e.toString());
  alert('jQuery.facebox(\"<div class=\'error\'>Error</div>\")');
  throw e;
}
like image 924
Hermine D. Avatar asked May 17 '10 14:05

Hermine D.


3 Answers

Have you tried putting the code in a partial? So instead of

render :js => "jQuery.facebox(#{...})"

try

render :partial => "my_facebox_popup" 

Then inside of your _my_facebox_popup.html.erb partial put your code:

<script type = "text/javascript">
...
</script>

debug any errors you get with firebug.

like image 152
Schneems Avatar answered Sep 20 '22 23:09

Schneems


Try this

 render :update do|page|
   page <<  "jQuery.facebox(#{...})"
 end
like image 36
Salil Avatar answered Sep 20 '22 23:09

Salil


Maybe you should specify in the jQuery call the dataType of the response you're waiting for.

E.g.:

$.ajax({ 
  url: "/controller/action/id", 
  success: function(){
    $(this).addClass("done");
  },
  dataType: 'script' 
});
like image 29
Stan Bright Avatar answered Sep 19 '22 23:09

Stan Bright