0
0

More than 3 years have passed since last update.

投稿機能の実装方法

Posted at

目次

①新規投稿ページに遷移可能にする
②データベースへ情報を登録可能にする

①新規投稿ページに遷移可能にする

投稿ページに飛べるようにnewアクションを呼び出すために、newアクションへのルーティングを設定する。

Rails.application.routes.draw do
 resoureces :human , only:[:new]
end

次にnewアクションを定義する

class HumansContorller < ApplicationController

  def new 
  end

そしてnew.html.erbに表示させたいようにビューを作成する。

form_withメソッドを使用すして、データの送信のビューを作成する

<%= form_with(model:モデルクラスのインスタンス) do |f| %>
  <%= f.text_field :name ,placeholder:"niconicocareer" %>
  <%= f.text_area :text,placeholder:"career adviser" %>
  <%= f.submit "送信"%>
<% end %>

◯補足(form_withでよく使われるメソッド)

メソッド 意味
label ラベルテキスト
check_box チェックボックス
password_field パスワード入力フィールド
submit 送信ボタン
text_area 複数行入力テキストエリア
text_field テキスト入力フィールド

②データベースへ情報を登録可能にする

createアクションに遷移するように、ルーティングを記述する

Rails.application.routes.draw do
 resoureces :human , only:[:new,:create]
end

次はcreateアクションを記述していく
※入力されたデータがしっかりと受け取れるように記述をする

class HumansContorller < ApplicationController

  def new 
  end

 def create
   Humnan.crate(input_date)
  end

 private
 def input_datta
   params.require(:human).permit(:name,:text)
 end

最後に投稿後のaction.html.erbのビューを作成し終了。

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