【问题标题】:alert close button is not showing in jquery .html() in ruby on railsruby on rails 中的 jquery .html() 中未显示警报关闭按钮
【发布时间】:2013-05-06 10:21:00
【问题描述】:

我在我的 rails 应用程序中使用 twitter 引导程序使用 AJAX crud 操作。当我单击提交帖子时,会显示成功闪烁消息,但没有显示关闭按钮。

这是我的代码...

<div id="flash_notice" class="alert span6"></div>

这是我的表格...

 <%= form_for(@post, :remote => true, :html => {:id => "prayer_form"}) do |f| %>
 <div id= "post_errors" style="display:none"></div>
 <p>
  <%= f.select :title, options_for_select(["Relationship", "Family", "Health",   "Personal", "Professional", "Education", "Tours & Travel", "Exams", "Job", "Society", "Career", "Marriage", "Relationship", "Friendship", "Event", "Success/Failure", "others"]), {:include_blank => "Select Prayer Category"}, {:multiple => false,:class => "required"} %>
 </p>
 <p>
  <%= f.text_area :content, :rows => 5, :class=>"span12 required", :placeholder => "Create your prayer" %>
 </p>
 <p><%= f.submit "Add Prayer", :class=>"btn btn-primary"%></p>
<% end %>

这是我的 create.js.erb

  $("#post_errors").hide(300);
  $("#flash_notice").html("<%= escape_javascript(flash[:notice] <button type='button' class='close' data-dismiss='alert'>&times;</button>) %>");
  $("#flash_notice").show(300);
  $(":input:not(input[type=submit])").val("");
  $("#posts_list").html("<%= escape_javascript( render(:partial => "posts") ) %>");

应用程序.js

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .

闪烁警报正在显示,但为什么我的关闭图标按钮没有显示。请帮忙

【问题讨论】:

  • 你的 application.js 是什么样的?你能发布你的代码示例吗

标签: jquery ruby-on-rails ajax ruby-on-rails-3 erb


【解决方案1】:

您无需将 escape_javascript 与真正的 html 代码混合使用。 HTML 代码不需要经过 Rails。

尝试替换这个

$("#flash_notice").html("<%= escape_javascript(flash[:notice] 
<button type='button' class='close' data-dismiss='alert'>&times;</button>) %>")

有了这个

$("#flash_notice").html("<%= escape_javascript(flash[:notice] %>" + 
"<button type='button' class='close' data-dismiss='alert'>&times;</button>")

【讨论】:

  • @user2354217,很好。原因是 Rails 将您的 html 转换为纯文本的“安全”html。
  • 但我遇到了一个问题,如果我关闭 Flash 消息并再次创建帖子,则 Flash 消息不会显示。需要刷新页面
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-06
  • 1970-01-01
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
  • 2013-07-24
相关资源
最近更新 更多