Javascript rails toastr:添加关闭按钮
我正在使用rails toastr gem(rails 5.0.0.1),我想在烤面包机上添加一个关闭按钮 我跟随并在Javascript rails toastr:添加关闭按钮,javascript,css,ruby-on-rails,toast,toastr,Javascript,Css,Ruby On Rails,Toast,Toastr,我正在使用rails toastr gem(rails 5.0.0.1),我想在烤面包机上添加一个关闭按钮 我跟随并在application.html.erb中添加了指向toastr.css和toastr.js的链接。然后我分别在app/assets/stylesheets和app/assets/javascripts下创建了两个文件,并在后者中添加了一行: toastr.options.closeButton = true; 但是烤面包机没有上来 我在application\u helper
application.html.erb
中添加了指向toastr.css
和toastr.js
的链接。然后我分别在app/assets/stylesheets
和app/assets/javascripts
下创建了两个文件,并在后者中添加了一行:
toastr.options.closeButton = true;
但是烤面包机没有上来
我在application\u helper.rb
中有这个方法(我在application.html.erb
中调用它):
def自定义\u引导\u闪存
flash_消息=[]
闪存。每个do |类型、消息|
类型='success'如果类型='notice'
类型='error'如果类型='alert'
text=“toastr.#{type}('#{message}');”
flash_messages我不知道,但它看起来已经过时,不再维护
我的问题与您的问题相同,但我通过使用另一个目前功能齐全且可以“查看”toastr选项解决了此问题。我希望它能帮助您或任何其他将面临此类问题的人。切勿使用这种方法,使用html\u-safe
是一个安全问题,rubocp
会投诉您的代码,即:
app/helpers/application_helper.rb:10:30: C: Rails/OutputSafety: Tagging a string as html safe may be a security risk.
flash_messages << text.html_safe if message
创建一个部分,例如\u toaster.html.erb
<%= content_tag(:script) do %>
<% flash.each do |type, message| %>
<%= toastr_flash_class("#{type}") %>('<%= message %>')
<% end %>
<% end %>
('')
从布局或视图调用您的局部视图:
<%= render 'layouts/shared/toastr' %>
将这些选项放在application.js文件中,或者如果您需要特定页面的其他选项,请放在特定的js/coffee文件中
application.js
...
//= require toastr
toastr.options = {
"closeButton": true,
"debug": false,
"progressBar": true,
"positionClass": "toast-top-right",
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
};
可以找到toastr的完整选项列表。只要运行演示,就会有一个输出文件显示您选择的选项。多亏了,我们选择了类似的解决方案。我们在app/assets/javascripts/helpers/
中添加了一个toastr\u override.js
文件,其中包含以下内容:
//= require toastr/toastr
toastr.options = Object.assign({}, toastr.options, {
closeButton: true
});
有同样的问题:(
...
//= require toastr
toastr.options = {
"closeButton": true,
"debug": false,
"progressBar": true,
"positionClass": "toast-top-right",
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
};
//= require toastr/toastr
toastr.options = Object.assign({}, toastr.options, {
closeButton: true
});