admin管理员组文章数量:1332881
I am using this library, () and I am trying to push my Flash notifications to the javascript function Toastr has provided for me. How do I call this function for every error or notification?
This is one of the methods that are used for making a toaster notification:
toastr.warning('This is a warning!')
I tried making a method in the ApplicationController to see if I could call that method on default errors from CanCan. I have various versions of the method, none of which worked.
def toast(type, text)
#if Logic here for various errors/notifications
respond_to do |format|
format.js { render action: "toastr.warning(#{text})", layout: false}
end
end
def toast(type, text)
#if Logic here for various errors/notifications
"toastr.warning(#{text})"
end
And then I try to use this method in the CanCan block:
rescue_from CanCan::AccessDenied do |exception|
toast :error, exception.message
redirect_to root_url
end
I would assume that this is possible, but I am just unsure how to implement it. Not many try to do this, and there is probably a reason. I am open to any suggestions on how to do what I am trying to do.
Here is a testing application that implements the Toast notifications: .html
I am using this library, (https://github./CodeSeven/toastr) and I am trying to push my Flash notifications to the javascript function Toastr has provided for me. How do I call this function for every error or notification?
This is one of the methods that are used for making a toaster notification:
toastr.warning('This is a warning!')
I tried making a method in the ApplicationController to see if I could call that method on default errors from CanCan. I have various versions of the method, none of which worked.
def toast(type, text)
#if Logic here for various errors/notifications
respond_to do |format|
format.js { render action: "toastr.warning(#{text})", layout: false}
end
end
def toast(type, text)
#if Logic here for various errors/notifications
"toastr.warning(#{text})"
end
And then I try to use this method in the CanCan block:
rescue_from CanCan::AccessDenied do |exception|
toast :error, exception.message
redirect_to root_url
end
I would assume that this is possible, but I am just unsure how to implement it. Not many try to do this, and there is probably a reason. I am open to any suggestions on how to do what I am trying to do.
Here is a testing application that implements the Toast notifications: http://codeseven.github.io/toastr/demo.html
Share Improve this question asked Sep 4, 2013 at 1:49 RizowskiRizowski 3,5985 gold badges24 silver badges31 bronze badges1 Answer
Reset to default 6What I'd remend is to make a new flash
type for this sort of thing and then render that as JS in your layout.
ApplicationController
def toast(type, text)
flash[:toastr] = { type => text }
end
app/views/layouts/<your layout>.html.erb
# (or in a partial to be reused in various layouts)
# the <script> tag isn't needed if code snippet is
# included in an existing script block, of course.
<% if flash[:toastr] %>
<script type="text/javascript">
<% flash[:toastr].each do |type, message| %>
toastr.<%= type %>(<%= message.inspect %>)
<% end %>
</script>
<% end %>
So this way you get all the standard behavior you're used to from the flash
object and you get easy to understand javascript written in your views directly via erb. You may need to add an options hash to the ApplicationController#toast
method so you can do a flash.now[:toastr]
at times, of course. And so on... But this should get you started.
本文标签: javascriptRails 4Toaster notifications rather than flash notificationsStack Overflow
版权声明:本文标题:javascript - Rails 4 - Toaster notifications rather than flash notifications - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742289327a2447506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论