Skip to main content

Command Palette

Search for a command to run...

Rails - form object

Published
2 min read

This is my note about how to implement a basic contact form using a form object.

dependencies used in the example

  • ruby 2.5.3
  • rails 5.2.2
  • webpacker
  • simple_form
  • slim_rails
  • etc

form object

# app/models/contact_form.rb

class ContactForm
  include ActiveModel::Model

  attr_accessor(
    :first_name,
    :last_name,
    :email
  )

  validates :first_name, presence: true
  validates :last_name, presence: true
  validates :email, presence: true

  def submit
    if valid?
      # do something if needed
      # - send notifications
      # - log events, etc.
      true
    else
      false
    end
  end
end

routing and controller actions

# config/routes.rb

Rails.application.routes.draw do
  resource :contact, only: [:show, :create]
end
# app/controllers/contacts_controller.rb

class ContactsController < ApplicationController
  def show
    @contact_form = ContactForm.new
  end

  def create
    @contact_form = ContactForm.new(contact_form_params)

    if @contact_form.submit
      flash[:notice] = "Thanks for contacting us"
      redirect_to root_url
    else
      render :show
    end
  end

  private def contact_form_params
    params.require(:contact_form).permit!
  end
end

view with contact form

/ app/views/contacts/show.slim

.container
  h1 Contact

  = simple_form_for @contact_form, url: contact_path do |f|
    = f.error_notification
    = f.input :first_name
    = f.input :last_name
    = f.input :email

    .btn-group
      = f.button :submit, 'Send Contact Form', class: "btn-primary"
      = f.button :button, "Cancel", type: "reset", class: "btn-outline-secondary"

Resources

  • https://robots.thoughtbot.com/activemodel-form-objects

More from this blog

Raspberry Pi TensorFlow Liteで物体検出を楽しむ

この記事について Raspberry Pi、TensorFlow、Pythonのいずれにも詳しくない筆者が、物体検出をやって楽しんだ成果の記録です。 TensorFlow公式の物体検出のサンプルプログラムを実行します。 動作環境 ボード Raspberry Pi 4 Model B OS Raspberry Pi OS (32-bit または 64-bit) デスクトップ環境 カメラ Raspberry Pi カメラモジュール v2 Python Python ...

Apr 23, 20231 min read

Elixir Circuits.I2C with Mox

This is written in Japanese. I might convert it to English later, maybe. はじめに Elixirのテストでモックを用意するときに利用するElixirパッケージとして、moxが人気です。Elixir作者のJosé Valimさんが作ったからということもありますが、ただモックを用意するだけではなくElixirアプリの構成をより良くするためのアイデアにまで言及されているので、教科書のようなものと思っています。 一言でいうと「その場...

Dec 3, 20213 min read
M

Masatoshi Nishiguchi's Blog

62 posts