导轨上的红宝石中的Bootstrap模式不起作用(不显示)

时间:2017-11-25 23:55:30

标签: jquery ruby-on-rails ruby twitter-bootstrap

我正在使用rail4和' bootstrap-sass':' 3.3.6'。

我已按照https://coderwall.com/p/ej0mhg/open-a-rails-form-with-twitter-bootstrap-modals中的步骤操作,并根据Bootstrap modal in ruby on rails not working重写了部分内容。

局部模板:

<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">Modal header</h3>
      </div>
      <div class="modal-body">
        <p>One fine body…</p>
      </div>
      <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
        <button class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

rails console中没有错误。

来自chrome检查的回复似乎没问题。

响应:

$("#myModal").find(".modal-content").html("<div aria-hidden=\'true\' aria-labelledby=\'myModalLabel\' class=\'modal fade\' id=\'myModal\' role=\'dialog\' tabindex=\'-1\'>\n<div class=\'modal-dialog\' role=\'document\'>\n<div class=\'modal-content\'>\n<div class=\'modal-header\'>\n<h3 id=\'myModalLabel\'>Modal header<\/h3>\n<\/div>\n<div class=\'modal-body\'>\n<p>One fine body…<\/p>\n<\/div>\n<div class=\'modal-footer\'>\n<button aria-hidden=\'true\' class=\'btn\' data-dismiss=\'modal\'>Close<\/button>\n<button class=\'btn btn-primary\'>Save changes<\/button>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n");
$("#myModal").modal('show');

但模式并没有出现。

其他相关代码:

调用该操作的视图:

%li= link_to 'Add release', new_release_path,  {:remote => true, 'data-toggle' =>  "modal", 'data-target' => '#myModal'}

new_release.js.erb

$("#myModal").find(".modal-content").html("<%= j (render 'new_release') %>");
$("#myModal").modal('show');

路线:

get "new_release" => 'project#new_release', :as => :new_release

3 个答案:

答案 0 :(得分:2)

请查看以下代码

JPanel

这意味着您尝试在另一个panel = new JPanel() { private static final long serialVersionUID = 1L; public void paintComponent(Graphics g) { super.paintComponent(g); a[0][0].northWall = false; a[mazeSize - 1][mazeSize - 1].southWall = false; for (int y = 0; y < mazeSize; y++) { for (int x = 0; x < mazeSize; x++) { if (a[x][y].northWall) { g.drawLine(100 + (x * 25), 100 + (y * 25), 100 + (x * 25) + 25, 100 + (y * 25)); } if (a[x][y].eastWall) { g.drawLine(100 + (x * 25) + 25, 100 + (y * 25), 100 + (x * 25) + 25, 100 + (y * 25) + 25); } if (a[x][y].southWall) { g.drawLine(100 + (x * 25), 100 + (y * 25) + 25, 100 + (x * 25) + 25, 100 + (y * 25) + 25); } if (a[x][y].westWall) { g.drawLine(100 + (x * 25), 100 + (y * 25), 100 + (x * 25), 100 + (y * 25) + 25); } } } } }; 元素中嵌套以$("#myModal").find(".modal-content").html("<div aria-hidden=\'true\' aria-labelledby=\'myModalLabel\' class=\'modal fade\' id=\'myModal\' role=\'dialog\' tabindex=\'-1\'>\n<div class=\'modal-dialog\' role=\'document\'>\n<div class=\'modal-content\'>\n<div class=\'modal-header\'>\n<h3 id=\'myModalLabel\'>Modal header<\/h3>\n<\/div>\n<div class=\'modal-body\'>\n<p>One fine body…<\/p>\n<\/div>\n<div class=\'modal-footer\'>\n<button aria-hidden=\'true\' class=\'btn\' data-dismiss=\'modal\'>Close<\/button>\n<button class=\'btn btn-primary\'>Save changes<\/button>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n"); $("#myModal").modal('show'); 元素开头的块。因此,结果HTML将包含具有相同ID的2个元素。首先尝试修复它。

答案 1 :(得分:1)

您应该使用$("#myModal").modal('show');打开模式。

答案 2 :(得分:1)

我猜你试图通过javascript在 new_release.js.erb 中使用

渲染该模态
$("#myModal").find(".modal-content")

实际上它没有加载到DOM中,因为未加载部分

所以不要在你的内部添加整个模态,而是在实际调用的模板文件中保留模型div标签,比如说你的主模板是

  

index.html.erb

<%= link_to 'Add release', new_release_path, :remote => true %>

<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content"></div>
  </div>
</div>

在js.erb里面

  

new_release.js.erb

$("#myModal").find(".modal-content").html("<%= j (render 'new_release') %>");
$("#myModal").modal('show');
  

_new_release.html.erb

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Modal header</h3>
  </div>
  <div class="modal-body">
    <p>One fine body…</p>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button class="btn btn-primary">Save changes</button>
  </div>

希望这有效,谢谢