LoginSignup
0
0

More than 3 years have passed since last update.

ヘルパーメソッドform_withについて理解する

Posted at

form_withとは

form_withとはRailsのヘルパーメソッドの一つで、ビューファイルに記述します。
このメソッドによって、情報を送信することができます。

form_withを使うと、入力フォームに必要なHTMLを作成することができます。

調べてみると、送信した情報をデータベースに保存する時と保存しない時では若干記述が変わるみたいです。

データベースに保存しない時

xxx.html.erb
<%= form_with url: "パス" do |f| %>
フォーム内容
<% end %>

データベースに保存する時

xxx.html.erb
<%= form_with(model: モデル名, local: true) do |f| %>
フォーム内容
<% end %>

form_withでは、データを保存しない
input typeの要素も使えるし、
データを保存するヘルパーメソッド(f.text_fieldなど)も両方使えるようです。

最後に、実際に今日実装してた内容を抜粋して記録として残しておきます。

記述例として参考になれば幸いです。

xxx.html.erb
<div class="items-sell-main">
    <h2 class="items-sell-title">商品の情報を入力</h2>
    <%= form_with(model: @product, local: true) do |f| %>
---省略---
    <%= f.text_area :name, class:"items-text", id:"item-name", placeholder:"商品名(必須 40文字まで)", maxlength:"40" %>
      <div class="items-explain">
        <div class="weight-bold-text">
          商品の説明
          <span class="indispensable">必須</span>
        </div>
---省略---
</div>
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