我正在使用一个链接来触发ajax,该ajax应该会弹出一个窗口,但我无法做到这一点。我正在使用gem'bootstrap-modal-rails',并且正在尝试以下代码
// index / html.erb
<%= link_to 'Link', load_users_teams_path(format: "js"), remote: true %>
<div id="id"></div>
<div id="modal-window" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>
//控制器
def load_users
@user = User.all
respond_to do |format|
format.html
format.js
end
end
// load_users.js.erb
$("#modal-window").html("<%= escape_javascript( render 'teams/load_users' ) %>");
$("#modal-window").modal()
// load_users.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">
**here comes whatever you want to show!**
</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>
已经厌倦了一个星期的尝试。.请指导我
答案 0 :(得分:0)
我在gemfile中添加了一些宝石
gem 'jquery-rails'
gem 'bootstrap-sass', '~> 3.4.0'
gem 'sassc-rails', '>= 2.0.0'
gem 'bootstrap-modal-rails'
在application.js中提到了这些
//= require rails-ujs
//= twitter-bootstrap
//= require activestorage
//= require turbolinks
//= require jquery
//= require bootstrap-modal
//= require bootstrap-modalmanager
在application.js中提到了这些
*= require_tree .
*= require_self
*= require bootstrap-modal-bs3patch
*= require bootstrap-modal
更改了application.html.erb的主体
<body>
<%= render 'layouts/header' %>
<div class="page-wrap">
<div class="span9"><%= yield %></div>
<div id="modal-window" class="modal fade" role="dialog" data-backdrop="static" style="z-index: 9999;" ></div>
</div>
<div id="modal-window" class="modal fade" role="dialog" style="z-index: 9999;" ></div>
<%= render 'layouts/footer' %>
</body>
_load_user.html.erb
<div class="modal-header">
<div class="modal-window">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Modal header</h3>
<div class="modal-body">
here comes whatever you want to show!
</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>
load_user.js.erb
$("#modal-window").html("<%= escape_javascript( render "load_users") %>");
$("#modal-window").modal();
new.html.erb
<%= link_to 'Link' ,load_users_teams_path, remote: true %>
这解决了我的问题,实际上我在new.html.erb上的代码与application.html.erb文件冲突,并且缺少一些宝石