Building a Contact Form Application with Ruby on Rails
As a software developer, you may often need to build web applications that require a contact form to collect user inquiries or feedback. In this post, we’ll walk through the steps of building a contact form application using Ruby on Rails.
Step 1: Generate a new Rails application
First, we need to generate a new Rails application. Open your terminal and run the command:
$ rails new contact_form
Step 2: Create a controller and view for the contact form
Next, we’ll create a new controller and a view for the contact form. In the terminal, run the rails generator command to generate a new controller and view.
$ rails generate controller contact_form index
class ContactFormController < ApplicationController
def index
@contact = Contact.new
end
end
<!-- app/views/contact_form/index.html.erb -->
<%= form_for @contact, url: contacts_path do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %><br>
<%= f.email_field :email %>
</p>
<p>
<%= f.label :message %><br>
<%= f.text_area :message %>
</p>
<p>
<%= f.submit 'Submit' %>
</p>
<% end %>
Step 3: Define the create action in the ContactController
We’ll define the create action in the ContactController to handle the form submission. This action will create a new Contact object with the form data, save it to the database, and send an email notification to the site owner using the ContactMailer.
class ContactsController < ApplicationController
def create
@contact = Contact.new(contact_params)
if @contact.save
# send email notification to site owner
ContactMailer.contact_email(@contact).deliver_now
redirect_to root_path, notice: 'Thank you for your message!'
else
render :index
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :message)
end
end
Step 4: Define the ContactMailer
We’ll define the ContactMailer to send email notifications to the site owner. This mailer will use the ActionMailer framework provided by Rails to send emails.
class ContactMailer < ApplicationMailer
def contact_email(contact)
@contact = contact
mail(to: 'site_owner@example.com', subject: 'New contact message')
end
end
Step 5: Create a new mailer view
Create a new mailer view at app/views/contact_mailer/contact_email.html.erb to define the email template.
<!-- app/views/contact_mailer/contact_email.html.erb -->
<h1>New Contact Message</h1>
<p>Name: <%= @contact.name %></p>
<p>Email: <%= @contact.email %></p>
<p>Message: <%= @contact.message %></p>
Step 6: Update the routes
Finally, update the routes.rb file to define the routes for the contact form.
Rails.application.routes.draw do
root 'contact_form#index'
resources :contacts, only: [:create]
end
That’s it! With these steps, you can now build a contact form application using Ruby on Rails. This application can be customized to fit your specific needs by adding additional fields, validations
Greetings, my name is Kirk. I’m the creator of Rails Zone and a passionate Ruby on Rails developer. During my free time, I develop helpful tools and tutorials that aid fellow Ruby on Rails developers in creating applications with improved efficiency and speed.