Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 4 bootstrap 3 ajax modal

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">&times;          </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.

like image 981
drac Avatar asked Mar 03 '14 09:03

drac


1 Answers

You also need to show the modal, add $('#myModal').modal('show') in show.js.erb

$("#myModal").html("<%= escape_javascript(render 'modal') %>");
$('#myModal').modal('show')
like image 135
Ishank Gupta Avatar answered Sep 21 '22 12:09

Ishank Gupta