Rails5.2.1でbootstrap4を使って簡単にモーダルを表示させてみましょう。
Rails5.2.1+bootstrap4でモーダル表示
以下のサイトを参考にします。
こんな↑モーダルが出ることが目標!(ボタンを押したらモーダル表示がされる)
まずは、bootstrapよりサンプルコードをコピー。(以下)
HTML
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
変更部分は、<!-- Modal -->
のidを今のモーダルに合わせて変更。(たとえば、id=”
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">addApp
“)
あとはボタンを合わせて修正。Railsなら以下のようにすればOKです。
ポイントは data: {target: "#addApp", toggle: "modal"}
を付けるのを忘れないことですね!
HTML
<%= button_tag(type: "button", class: "mr-1 btn btn-warning", data: {target: "#addApp", toggle: "modal"}) do %> XXXボタン名XXX <% end %>
これでボタンをおせばモーダル表示ができます!(*^^*)
参考になれば幸いです。