[RUBY] Implement a contact form in Rails

I implemented an inquiry form using Gmail in Rails.

Routing settings

First, set up the routing. It is assumed that when you enter the inquiry content on the inquiry form page and send it, you will be taken to the transmission completion page.


Rails.application.routes.draw do
  resource :contacts, only: [:new, :create] do
    get "/thanks" => "contacts#thanks"

Creating a model

The contents to be entered in the inquiry form are three points: name, email address, and inquiry contents.

$ rails g model contact name:string email:string content:text
$ rails db:migrate

Creating a controller

$ rails g controller contacts


class ContactsController < ApplicationController
  def new
    @contact = Contact.new

  def create
    @contact = Contact.new(contact_params)
    if @contact.save
      redirect_to thanks_contacts_path
      render :new

  def thanks

  def contact_params
    params.require(:contact).permit(:name, :email, :content)

Creating a view (contact form)

There are some hiragana parts that match the taste of the site I created, but don't worry.


<h3>Contact Us</h3>
<%= form_with(model: @contact, local: true) do |f| %>
  <%= f.label :name, "name" %>
  <%= f.text_field :name %>
  <%= f.label :email, "mail address" %>
  <%= f.email_field :email, autocomplete: "email" %>
  <%= f.label :content, "Not like" %>
  <%= f.text_field :content %>
  <%= f.submit "Come" %>
<% end %>

Creating a view (send completion screen)


<h3>Thank you for your inquiry.</h3>

Creating a mailer

$ rails g mailer ContactMailer

Describe the mail sending process


class ContactMailer < ApplicationMailer
  def contact_mail(contact)
    @contact = contact
    mail to:"My email address", subject: "Contact Us"

Creating an email


<p>User name:<%= @contact.name %></p>
<p>mail address:<%= @contact.email %></p>
<p>Content of inquiry:<%= @contact.content %></p>

Gmail server settings


Rails.application.configure do
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: 'smtp.gmail.com',
    domain: 'gmail.com',
    port: 587,
    user_name: 'My email address',
    password: ENV["GMAIL_KEY"],
    authentication: 'plain',
    enable_starttls_auto: true

I'll put the password in my .env file so that it doesn't go up on GitHub.

Gmail key settings

Describe the following in the .env file (created if it does not exist) directly under the application directory.


For the password entered here, access Google Account and enter the generated app password.

Security → Google login field If you have not set up 2-step authentication, first set it up and then generate an app password. Enter the password generated here.

Implementation is complete here.

Now when you send an email from the inquiry form スクリーンショット 2020-10-25 23.32.31.png

I was able to send and receive safely.

Recommended Posts

Implement a contact form in Rails
Implement markdown in Rails
How to implement a like feature in Rails
Implement application function in Rails
Implement follow function in Rails
Implement import process in Rails
How to implement a like feature in Ajax in Rails
Add a search function in Rails.
3 Implement a simple interpreter in Java
Implement simple login function in Rails
Create a new app in Rails
Implement tagging function in form object
Implement a gRPC client in Ruby
Implement CSV download function in Rails
[Rails] A simple way to implement a self-introduction function in your profile
How to insert a video in Rails
Steps to set a favicon in Rails
Implement something like a stack in Java
How to implement ranking functionality in Rails
Implement button transitions using link_to in Rails
[Rails] I want to send data of different models in a form
Implement Rails pagination
Group_by in Rails
Convert to a tag to URL string in Rails
Implement user management functions in a wizard format
Implement star rating function using Raty in Rails6
Rails logger Get a rough idea in 1 minute
How to easily create a pull-down in Rails
Implement the Like feature in Ajax with Rails.
(Ruby on Rails6) Creating data in a table
Implement iteration in View by rendering collection [Rails]
How to make a follow function in Rails
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
How to implement guest login in 5 minutes in rails portfolio
One case of solving a migration error in Rails
Implement post search function in Rails application (where method)
Ruby on Rails Incorrect string value error resolution when posting a form in Japanese
Model association in Rails
Adding columns in Rails
Introduce devise in Rails to implement user management functionality
Implement a multi-column IN clause (column: condition = n: n) in DOMA.
[Rails] Implement search function
Disable turbolinks in Rails
I want to use a little icon in Rails
Throw raw SQL to a read replica in Rails
[Ruby / Rails] Set a unique (unique) value in the class
[How to insert a video in haml with Rails]
Implement Rails account BAN
Quickly implement a singleton with an enum in Java
How to write a date comparison search in Rails
I want to define a function in Rails Console
^, $ in Rails regular expression
Implement user follow function in Rails (I use Ajax) ①
Use images in Rails
[Rails 6] How to set a background image in Rails [CSS]
Implement CustomView in code
Understand migration in rails
[Rails] How to load JavaScript in a specific view
Split routes.rb in Rails6
How to implement a circular profile image in Rails using CarrierWave and R Magick
[Rails] Implement rake task