rine
rine

Reputation: 13

Rails: Choosing an object on button click and showing info on modal

I have a long list of cats in my database

Cat(id: integer, name: string, gender: integer, home: boolean, created_at: datetime, updated_at: datetime)

and on my web page I wish to open a Bootstrap modal with the information of only one cat in it. The id of the cat would be determined on button click. In my CatsController I have defined

def index
 @cats = Cat.all
end

and the path /cats/ shows

<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal"> Open Modal </button>

  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>Some text in the modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>

How can this be done the Rails way?

Upvotes: 1

Views: 1027

Answers (1)

plombix
plombix

Reputation: 406

Make a loop to iterate over cats , in the loop body include your modal and your button .

Inside your button put a :

data-target="#myModal_<%=cat.id%>">

And inside your modal put a

 id="myModal_<%=cat.id%>"

I'm afraid this is a matter of client side and rails is more server oriented .

Upvotes: 1

Related Questions