LoginSignup
38
50

More than 5 years have passed since last update.

【Rails】form_forの使い方

Last updated at Posted at 2017-01-24

form_forとは

モデルの新規インスタンスに値を追加して保存したい時に使用するヘルパーメソッド。
フォームが簡単に作成でき、入力されたデータをテーブルに保存できる。

フォームの作成

レビューのフォームを想定。

コントローラーの実装

reviews_controller.rb
  def new
    @review = review.new
  end

  def create
    Review.create(create_params)
    render action: :new #表示したいビューを表示
  end

  private
  def create_params
    params.require(:review).permit(:text)
  end

ビューの実装

new.html.haml
%h1 Review
  = form_for @review do |f|
    = f.label :text, "レビュー"
    = f.text_field :text
    = f.submit "Send"

・モデルのインスタンスを引数に持つ。

・引数のインスタンスが何も情報を持っていなければcreateアクション、すでに情報を持っている場合はupdateアクションに自動的に振り分けられる。

・フォームに入力された値は、submitボタンを押した瞬間にモデルクラスの新規インスタンスにそれぞれの属性の値としてセットされ、対応するテーブルのカラムに保存される。

ネストをした場合

groupsとmessagesが1対多関係。
ネストした場合、引数のインスタンスは二つになる。

config/routes.rb
resources :groups do 
  resources :messages
end
message/new.html.haml
= form_for [@group, @message] do |f|
...
38
50
1

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
38
50