2016-07-25 62 views
0

好的,我有一个链接到HTML模式,它完美的作品。 正如你可以看到那里的链接,但我不需要它在我的应用程序。Rails,Bootstrap,试图动态加载模态不能按预期工作

<div class="block"> 
    <a data-toggle="modal" class="btn btn-danger btn-block" role="button" href="#editor-modal">Run modal with WYSIWYG editor</a> 

    <!-- Modal with WYSIWYG editor --> 
    <div aria-hidden="true" style="display: none;" id="editor-modal" class="modal fade" tabindex="-1" role="dialog"> 
     <div class="modal-dialog"> 
      <div class="modal-content"> 
       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
        <h4 class="modal-title"><i class="icon-quill2"></i> WYSIWYG editor inside modal</h4> 
       </div> 



       <div class="modal-footer"> 
        <button class="btn btn-warning" data-dismiss="modal">Close</button> 
        <button class="btn btn-primary">Save</button> 
       </div> 
      </div> 
     </div> 
    </div> 
    <!-- /modal with WYSIWYG editor --> 

</div> 

而不是在我的应用程序中有大量的模态,我想动态加载它们。

我试着定义一个占位符并在那里加载模态,然后显示它。 我已经做了几次,但这次由于某种原因它不起作用。

所以在我index.html.erb

<div id="modal-placeholder" class="block"> </div> 

我有我的部分_addNewModal.html.erb

<!-- Modal with WYSIWYG editor --> 
<div aria-hidden="true" style="display: none;" id="editor-modal" class="modal fade" tabindex="-1" role="dialog"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
       <h4 class="modal-title"><i class="icon-quill2"></i> WYSIWYG editor inside modal</h4> 
      </div> 



      <div class="modal-footer"> 
       <button class="btn btn-warning" data-dismiss="modal">Close</button> 
       <button class="btn btn-primary">Save</button> 
      </div> 
     </div> 
    </div> 
</div> 
<!-- /modal with WYSIWYG editor --> 

我用下面的代码加载。

$('#modal-placeholder').html("<%= j render 'news/modals/addNewModal'%>") 
$('#editor-modal').modal('toggle'); 

我不明白这里有什么问题。我试图复制我的模板的HTML代码,但由于某种原因,它不起作用。

任何帮助表示赞赏。

编辑: 当我被选中,模式占位符实际上充满了数据,但该模式不显示up.I都尝试

$('#editor-modal').modal('toggle'); 

&

$('#editor-modal').modal('toggle'); 

任何想法?

回答

1

j通过一个js.erb文件默认搜索渲染

当渲染Javascript代码里面的谐音,你应该使用escape_javascript方法,像这样

$('#modal-placeholder').html("<%= escape_javascript render(:partial => 'news/modals/addNewModal') %>");

如果明确申报partial密钥,那么它将搜索您的html.erb file instead of js.erb

因此,请尝试上述命令或此。

$('#modal-placeholder').html("<%= j render(:partial => 'news/modals/addNewModal') %>");

对于来回切换,从表单中删除内嵌display: none;并添加一个风格吧,

#editor-modal 
{ 
    display: none; 
} 

然后切换会工作。

$('#editor-modal').toggle(); 
+0

谢谢你的回答,因为我现在检查它加载数据,无论是用我的代码还是你的,但'切换'不起作用。我将编辑该问题。 –

+1

从内联中删除'display:none'并将其添加到样式中, '#editor-modal { display:none; }',那么切换也将工作。 – Sravan

+0

仍然无法使用。看起来像读取bootstrap.min.js的问题。 它不会改变类,显示等,就像我得到的html模板一样。有关于此的任何想法? –