Cannot display JavaScript from a Rails controller

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

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

only the actual JavaScript code is displayed:

 try { jQuery.facebox(...) } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('jQuery.facebox(\"<div class=\'error\'>Error</div>\")'); throw e; } 
+6
ruby-on-rails rjs
source share
3 answers

Have you tried to put the code in partial? Therefore, instead of

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

try

 render :partial => "my_facebox_popup" 

Then, inside your _my_facebox_popup.html.erb partial code, type:

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

debug any errors you get with firebug .

+7
source share

try it

  render :update do|page| page << "jQuery.facebox(#{...})" end 
+1
source share

You might need to specify the response data type that you expect in a jQuery call.

eg:.

 $.ajax({ url: "/controller/action/id", success: function(){ $(this).addClass("done"); }, dataType: 'script' }); 
+1
source share

All Articles