0
0

More than 1 year has passed since last update.

bootstrap modal とform_withの組み合わせ

Posted at

bootstrapのmodalにform_withを組み込めないか検証してみた。
結果、上手く機能した。(unreadableで申し訳ないが以下のコードで一応機能した。)

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
アカウントを作成する
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">入力画面</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
        <div class="modal-body">
        <%= form_with(model: @user, local: true) do |f| %>
        <%= f.label :name %>
        <%= f.text_field :name %>
        <%= f.label :email %>
        <%= f.email_field :email %>
        <%= f.label :password %>
        <%= f.password_field :password %>
        <%= f.label :password_confirmation, "Confirmation" %>
        <%= f.password_field :password_confirmation %>
        </div>
        <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">閉じる</button>
        <%= f.submit "アカウントを作成する", class: "btn btn-primary" %>
        <% end %>
      </div>
    </div>
  </div>
</div>

特にhtmlに展開する形は押さえておきたい。

<%= f.text_field :name %>
<label for="user_name">Name</label>
<input id="user_name" name="user[name]" type="text" />

<%= f.label :email %>
<label for="user_email">Email</label>

<%= f.email_field :email %>
<input id="user_email" name="user[email]" type="email" />

<%= f.submit "Create my account", class: "btn btn-primary" %>
<input class="btn btn-primary" name="commit" type="submit"
value="Create my account" />

name=XXXに指定している名前、例えばuser[email]にしているからこそuser.emailで取り出し、newアクション => createアクションに飛ばせる(?)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0