我正在尝试在我的flash消息上应用javascript,但它并没有消失。我尝试在同一页面和application.js上应用代码,但flash消息并不消失。
[article_controller.rb]
class ArticlesController < ApplicationController
before_action :set_params,only: [:edit,:update,:show,:destroy]
def index
@articles=Article.all
end
def new
@article=Article.new
end
def edit
end
def create
@article=Article.new(article_params)
@article.user=User.first
if @article.save
flash[:success]="Article was successfuly created"
redirect_to article_path(@article)
else
render 'new'
end
end
def show
end
def update
if @article.update(article_params)
flash[:success]="Article was successfuly updated"
redirect_to article_path(@article)
else
render 'new'
end
end
def destroy
@article.destroy
flash[:danger]="Article was successfuly deleted"
redirect_to articles_path
end
private
def set_params
@article=Article.find(params[:id])
end
def article_params
params.require(:article).permit(:title,:description)
end
end
[_message.html.erb]
<script type="text/javascript">
$(document).ready(function(){
setTimeout(function(){
$('#flash').remove();
}, 5000);
})
</script>
<div class="row">
<div class="col-xs-10 col-xs-offset-1">
<% flash.each do |name,msg| %>
<div class='alert alert-<%="#{name}" %>'>
<a href="#" class="close" data-dismiss="alert">×</a>
<%= content_tag :div,msg, :id=>"flash_#{name}" if msg.is_a?(String) %>
</div>
<% end %>
</div>
</div>
[application.html.erb]
<!DOCTYPE html>
<html>
<head>
<title>BlogApplication</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= render 'layouts/navigation' %>
<%= render 'layouts/messages' %>
<div class="container">
<%= yield %>
</div>
<%= render 'layouts/footer' %>
</body>
</html>
答案 0 :(得分:1)
似乎没有任何#flash id你试图删除:
db.sample.find( { ids: { $elemMatch: { $lt: 4, $gt: 3 } } } )
您可能需要:
$('#flash').remove();
或者更好的是,因为那里有一些
$('.flash').remove();
<%= content_tag :div,msg, :class => 'flash', ...
以上
答案 1 :(得分:1)
你可以使用jQuery或JS自动删除flash消息,我会给你一个示例代码,只需通过它并在代码中进行必要的修改:)
所有
共同$(".alert" ).fadeOut(3000);
警报成功:
$(".alert-success" ).fadeOut(3000);
警告危险:
$(".alert-danger" ).fadeOut(3000);