Reputation: 357
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:
<%= 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>
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>
also a works controller:
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
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)
Any help on this is greatly appreciated.
Upvotes: 12
Views: 12960
Reputation: 1593
You also need to show the modal, add $('#myModal').modal('show')
in show.js.erb
$("#myModal").html("<%= escape_javascript(render 'modal') %>");
$('#myModal').modal('show')
Upvotes: 5
Reputation: 5734
Have a try with another way. In this way, we are going to show the modal popup using explicit javascript command.
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>
In application.js
$(document).ready(function() {
var clickOnPopupLink = function(){
$('body').on('click', '.static-popup-link', function(){
$('#myModal').modal('show');
});
}
clickOnPopupLink();
});
In controller
def show
@work = Work.find(params[:id])
end
In show.js.erb
$('#myModal').html("<%= j render("/works/modal")%>")
Upvotes: 17