javascript - Rails 4 - Toaster notifications rather than flash notifications -


i using library, (https://github.com/codeseven/toastr) , trying push flash notifications javascript function toastr has provided me. how call function every error or notification?

this 1 of methods used making toaster notification:

toastr.warning('this warning!') 

i tried making method in applicationcontroller see if call method on default errors cancan. have various versions of method, none of worked.

def toast(type, text)     #if logic here various errors/notifications     respond_to |format|         format.js { render action: "toastr.warning(#{text})", layout: false}     end end  def toast(type, text)     #if logic here various errors/notifications     "toastr.warning(#{text})" end 

and try use method in cancan block:

rescue_from cancan::accessdenied |exception|     toast :error, exception.message     redirect_to root_url end 

i assume possible, unsure how implement it. not many try this, , there reason. open suggestions on how trying do.

here testing application implements toast notifications: http://codeseven.github.io/toastr/demo.html

what i'd recommend make new flash type sort of thing , render js in layout.

applicationcontroller  def toast(type, text)   flash[:toastr] = { type => text } end   app/views/layouts/<your layout>.html.erb # (or in partial reused in various layouts) # <script> tag isn't needed if code snippet # included in existing script block, of course.  <% if flash[:toastr] %>   <script type="text/javascript">     <% flash[:toastr].each |type, message| %>       toastr.<%= type %>(<%= message.inspect %>)     <% end %>   </script> <% end %> 

so way standard behavior you're used flash object , easy understand javascript written in views directly via erb. may need add options hash applicationcontroller#toast method can flash.now[:toastr] @ times, of course. , on... should started.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -