rails 4 bootstrap 3 ajax modal [英] rails 4 bootstrap 3 ajax modal
问题描述
我正忙着学习Rails 4,当用户点击链接时我想显示一个bootstrap popup模式,当模态打开时,它需要显示与该特定记录相关的信息。
以下是我到目前为止所做的事情,它只是没有显示实际的模态弹出窗口但是确实传递了正确的参数。
I'm busy learning Rails 4 and I would like to display a bootstrap popup modal when a user clicks on a link, when the modal opens it needs to show the information relating to that particular record. Heres what I have done so far, which just doesn't show the actual modal popup BUT does pass the correct parameters.
index.html.erb page has:
index.html.erb page has:
<%= link_to "view", work_path(w), remote: true, :"data-toggle" => 'modal', :"data-target" => '#myModal' %>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>
我还有作品/ _modal.html.erb:
I also have a works/_modal.html.erb:
<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" id="myModalLabel"></h4>
</div>
<div class="modal-body">
<%= @work.name %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
也是工程控制人:
def show
@work = Work.find(params[:id])
if request.xhr?
respond_to do |format|
format.html {render :partial => 'modal'}
format.json {head :ok}
end
end
end
最后是作品/ show.js.erb:
and finally a works/show.js.erb:
$("#myModal").html("<%= escape_javascript(render 'modal') %>");
我希望我在这里错过了一些简单的东西。在控制台中我可以看到以下消息,所以我知道它返回正确的信息,但不幸的是它没有显示模态。:
I hope i'm missing something easy here. in the console I can see the following message so i know it is returning the correct information, but unfortunately it is not showing the modal.:
Started GET "/works/8" for 127.0.0.1 at 2014-03-03 09:31:12 +0000
Processing by WorksController#show as JS
Parameters: {"id"=>"8"}
Work Load (0.2ms) SELECT "works".* FROM "works" WHERE "works"."id" = ? LIMIT 1 [["id", 8]]
Rendered works/_modal.html.erb (4.9ms)
Completed 200 OK in 8ms (Views: 6.3ms | ActiveRecord: 0.2ms)
非常感谢任何帮助。
推荐答案
尝试另一种方式。通过这种方式,我们将使用显式javascript命令显示模态弹出窗口。
Have a try with another way. In this way, we are going to show the modal popup using explicit javascript command.
在index.html.erb
In index.html.erb
<%= link_to "view", work_path(w), remote: true, class: 'static-popup-link'%>
<div class="modal hide fade" id="myModal" tabindex="-1" role="dialog" data-backdrop="static" data-keyboard="false">Loading...</div>
在application.js中
In application.js
$(document).ready(function() {
var clickOnPopupLink = function(){
$('body').on('click', '.static-popup-link', function(){
$('#myModal').modal('show');
});
}
clickOnPopupLink();
});
在控制器中
def show
@work = Work.find(params[:id])
end
在show.js.erb
In show.js.erb
$('#myModal').html("<%= j render("/works/modal")%>")
这篇关于rails 4 bootstrap 3 ajax modal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!