LoginSignup
0
0

More than 1 year has passed since last update.

入力フォームを作成する方法

Last updated at Posted at 2021-05-18

入力フォームを作成する方法

フォームの作成

<%= form_with model:@book,local:true do |f| %>
  <h4>Title</h4>
      <%= f.text_field :title, class: "form-control" %>
    <h4>Opinion</h4>
        <%= f.text_area :body, class: "form-control"  %>
        <%= f.submit 'Create Book',class: "btn btn-success my-3"%>
<% end %>

※上記の内容では、Bootstrap(CSSのデザインテンプレート)を使用

①<%= form_with model:@book,local:true do |f| %>
 ⇒@bookに入力フォームの内容を保存することを示している

②<%= f.text_field :title, class: "form-control" %>
 ⇒text.fieldとすることで、一行のテキスト投稿フォームができる。
またclass:と設定し入力フォームのデザインを決めている。

③<%= f.text_area :body, class: "form-control" %>
 ⇒text_area とすることで、複数行のテキスト投稿フォームができる。
※areaとfieldの違いに注意

④<%= f.submit 'Create Book',class: "btn btn-success my-3"%>
 ⇒submitでは、ボタンを生成する。
※myはBootstrapでのデザイン設定項目
 ・m - for classes that set margin
 ・y - for classes that set both *-top and *-bottom
 ・3 - (by default) for classes that set the margin or padding to spacer

spacer の基準値は1rem=16px。spacers Sassマップ変数にエントリを追加することで, さらにサイズの追加が可能です。

最終出来上がりイメージ

図9.png

画像を投稿する際

<%= f.label :image 'iamge' %>
<%= f.file_field :image %>

カレンダーを入れたい場合

<%= form.date_field :bodyなど %>

bootstrapの記述に関しては、https://getbootstrap.jp/docs/4.5/utilities/spacing/を参考にしました。

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