Rails display bootstrap modal of index from another model

Mark Locklear

I have two models groups and shots. From my groups #show page, I want a modal window that will allow me to display a list of all my shots. Here is the relevant code...

app/views/groups/show.html.erb

<%= link_to "Add Exsisting Shot", shots_path, id: "shot-modal", remote: true %>

app/views/shots/modal.js.erb

$("#shot-modal").html("<%= escape_javascript(render 'modal') %>")
$("#shot-modal").modal("show")

app/controllers/shots_controller.rb

 def index
    @shots = Shot.all
    respond_to do |format|
      format.html # index.html.erb
      format.js # index.html.erb
      format.json { render json: @shots }
    end
  end

When I click the 'Add Existing Shot' link, I do a a model that sort of turns the who screen gray, but I am not getting any content. I can click anywhere on the screen, it it brings me back to my show page. I'm also not getting any errors in the console.

EDIT

app/views/shots/_modal.html.erb

<div class="modal fade" id="shot-modal" tabindex="-1" role="dialog" aria-labelledby="shot-modal-label" aria-hidden="true">
  <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="shot-modal-label">Modal title</h4>
      </div>
      <div class="modal-body" id="shot-modal-body">
        <table>
          <tr>
            <th>Name</th>
            <th>Comedian</th>
            <th></th>
            <th></th>
            <th></th>
          </tr>

        <% @shots.each do |shot| %>
          <tr>
            <td><%= shot.name %></td>
            <td><%= shot.comedian.name %></td>
            <td><%= image_tag shot.pic.url(:thumb) %></td>
          </tr>
        <% end %>
        </table>
      </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>
</div>
mccannf

Two things:

1 - If you have a modal like this:

<div class="modal fade" id="product-modal" tabindex="-1" role="dialog" aria-labelledby="product-modal-label" aria-hidden="true">
  <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="product-modal-label">Modal title</h4>
      </div>
      <div class="modal-body" id="product-modal-body">
        ...
      </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>
</div>

Then you should not use $("#product-modal").html(... because you will overwrite a lot of the important divs in the modal. You should use $(".modal-body","#product-modal").html(... or set the id for the modal-body as above and use $("#product-modal-body").html(....

2 - The render won't work. When you run the render command like that from a view, it is looking for a partial. So your JavaScript should actually look like something like this:

$("#product-modal").html("<%= escape_javascript(render 'allshots') %>")

And then you should put the HTML for displaying your shots in app/views/shots/_allshots.html.erb.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to display a react-bootstrap modal from another component

From Dev

bootstrap dismiss modal and display another modal

From Dev

Bootstrap Modal from another page

From Dev

Bootstrap Modal from another page

From Dev

Bootstrap 3 - open modal from another modal

From Dev

Twitter bootstrap show modal from by another modal

From Dev

Rails Bootstrap modal trying to update wrong model

From Dev

How to trigger & load the display of an overlay/modal from a link from another page - Rails 5 Javascript

From Dev

How to display model fields from another model using nested resource rails 5

From Dev

Bootstrap modal on another modal

From Dev

Display Image from DB in Modal Bootstrap Window

From Dev

Open a bootstrap modal from another page

From Dev

Rails - Editing item from list by bootstrap modal

From Dev

rails fill model with data from another model

From Dev

Submit MVC Form Results to HttpPost method and display model in bootstrap modal

From Dev

Open a modal from another modal using bootstrap and angularJs

From Dev

Why bootstrap modal can't display on Rails 5?

From Dev

Bootstrap: Open Another Modal in Modal

From Dev

Rails Load Dropdowns from another model based on values in another model

From Dev

How to display from one model but save to another

From Dev

Display List of objects from another Model Django

From Dev

Displaying information from another model on index

From Dev

Rails Bootstrap Modal not appearing

From Dev

Ruby on Rails bootstrap modal

From Dev

Rails Bootstrap Modal not appearing

From Dev

Call a variable from another model in Ruby on Rails

From Dev

Using attributes from one model in another Rails

From Dev

Display data from another table rails

From Dev

Call Bootstrap 3 Modal from an MVC razor model?

Related Related

  1. 1

    How to display a react-bootstrap modal from another component

  2. 2

    bootstrap dismiss modal and display another modal

  3. 3

    Bootstrap Modal from another page

  4. 4

    Bootstrap Modal from another page

  5. 5

    Bootstrap 3 - open modal from another modal

  6. 6

    Twitter bootstrap show modal from by another modal

  7. 7

    Rails Bootstrap modal trying to update wrong model

  8. 8

    How to trigger & load the display of an overlay/modal from a link from another page - Rails 5 Javascript

  9. 9

    How to display model fields from another model using nested resource rails 5

  10. 10

    Bootstrap modal on another modal

  11. 11

    Display Image from DB in Modal Bootstrap Window

  12. 12

    Open a bootstrap modal from another page

  13. 13

    Rails - Editing item from list by bootstrap modal

  14. 14

    rails fill model with data from another model

  15. 15

    Submit MVC Form Results to HttpPost method and display model in bootstrap modal

  16. 16

    Open a modal from another modal using bootstrap and angularJs

  17. 17

    Why bootstrap modal can't display on Rails 5?

  18. 18

    Bootstrap: Open Another Modal in Modal

  19. 19

    Rails Load Dropdowns from another model based on values in another model

  20. 20

    How to display from one model but save to another

  21. 21

    Display List of objects from another Model Django

  22. 22

    Displaying information from another model on index

  23. 23

    Rails Bootstrap Modal not appearing

  24. 24

    Ruby on Rails bootstrap modal

  25. 25

    Rails Bootstrap Modal not appearing

  26. 26

    Call a variable from another model in Ruby on Rails

  27. 27

    Using attributes from one model in another Rails

  28. 28

    Display data from another table rails

  29. 29

    Call Bootstrap 3 Modal from an MVC razor model?

HotTag

Archive