Rails 4 render Gritter messages in js view

370 Views Asked by At

Using Rails 4.2.4, ruby 2.2.2, gritter gem 1.2.0. My controller respond with js:

#posts_controller.rb
if @post.update post_params
  gflash :success => "Link updated"
end

in accordion with official doc this is my js view:

#update.js.erb
<%= gflash :js => true %>

The problem is none is rendered, Rails server works fine and no problems are show in console. I have inspected the produced js and I have see this:

jQuery(function(){jQuery.gritter.add({image:'/assets/success-0b8a2dedd729f28e513472812f5483ae2817ae482c0b744c3ce56c9dd2b2bc1d.png',title:'
Success',text:'Link updated'});});

It seems the browser doesn't render the jQuery function. (I have jquery allready installed in my app) Ideas?

1

There are 1 best solutions below

0
On

Solution:

Short: <%= (gflash :js => true).html_safe %>

Long: Inspecting server response with Fiddler Web Debugger I have notice the response is like this:

jQuery(function(){jQuery.gritter.add({image:&#39;/assets/success-0b8a2dedd729f28e513472812f5483ae2817ae482c0b744c3ce56c9dd2b2bc1d.png&#39;,title:&#39;Success&#39;,text:&#39;Link updated&#39;});});

If I execute the code above in chrome console I have an error:

VM8474:2 Uncaught SyntaxError: Unexpected token 

So using the html_safe method works for me: <%= (gflash :js => true).html_safe %>