0
0

More than 3 years have passed since last update.

フォームに関する基礎知識をつける Ruby on Rails

Posted at

新規作成フォームと更新フォーム中身は同じ?でも何かが違う!

中身 = 入力欄が記載されている_form.html.erbなどのことです
違い = action 属性

下記のような_form.htmlはnewとedit両方に使用されます。(中身は同じ)


<table class="attr">
    <tr>
        <th><%= form.label :name, "ユーザー名"%></th>
        <td><%= form.text_field :name %></td>
    </tr>
    <tr>
        <th><%= form.label :birthday, "誕生日",
                    for: "member_birthday_li"%></th>
        <td><%= form.date_select :birthday,
                 start_year: 1970, end_year: Time.current.year,
                 use_month_numbers: true%></td>
    </tr>
    <tr>
        <th><%= form.label :email, "メールアドレス"%></th>
        <td><%= form.text_field :email %></td>
    </tr>
    <tr>
        <th>管理者</th>
        <td>
            <%= form.check_box :administrator %>
            <%= form.label :administrator, "管理者" %>
        </td>
    </tr>
</table>
<% @page_title = "会員の新規登録"%>

<h1><%= @page_title%></h1>

<%= form_for @member do |form|%>
    <%= render "form", form: form %>
    <div><%= form.submit %></div>
<%end%>

しかしページのソースを確認するとaction属性が違うのです!そもそも遷移しているURLが違うのだから当たり前だろと思うかもしれませんが、初心者の僕からした頭をこんがらせた要因でもありました。

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