LoginSignup
6
4

More than 5 years have passed since last update.

Ruby on Ralis ~ お問い合わせフォーム(コードメモ)

Last updated at Posted at 2016-12-21

1.routes

/config/routes.rb
Rails.application.routes.draw do
  get 'inquiry' => 'inquiry#index'              # 入力画面
  post 'inquiry/confirm' => 'inquiry#confirm'   # 確認画面
  post 'inquiry/thanks' => 'inquiry#thanks'     # 送信完了画面
end

2.controller

$ rails g controller inquiry
/app/controllers/inquiry_controller.rb
class InquiryController < ApplicationController

  def index
    # 入力画面を表示
    @inquiry = Inquiry.new
    render :action => 'index'
  end

  def confirm
    # 入力値のチェック
    @inquiry = Inquiry.new(params[:inquiry])
    if @inquiry.valid?
      # OK。確認画面を表示
      render :action => 'confirm'
    else
      # NG。入力画面を再表示
      render :action => 'index'
    end
  end

  def thanks
    # メール送信
    @inquiry = Inquiry.new(params[:inquiry])
    InquiryMailer.received_email(@inquiry).deliver

    # 完了画面を表示
    render :action => 'thanks'
  end

end

3.view

/app/views/inquiry/index.html.erb
<%= form_for @inquiry, :url => inquiry_confirm_path do |f| %>
  <div class="page-header">
    <h1>お問い合わせ</h1>
  </div>

  <% if @inquiry.errors.any? %>
    <div class="alert alert-danger" role="alert">
      <strong>入力内容にエラーがあります</strong>
      <ul>
        <% @inquiry.errors.each do |attr, msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <table class="table">
    <tr>
      <th>名前<span class="text-danger">(必須)</span></th>
      <td><%= f.text_field :name %></td>
    </tr>
    <tr>
      <th>メールアドレス<span class="text-danger">(必須)</span></th>
      <td><%= f.text_field :email %></td>
    </tr>
    <tr>
      <th>お問い合わせ内容</th>
      <td><%= f.text_area :message %></td>
    </tr>
  </table>
  <%= f.submit '確認', class: 'btn btn-primary' %>
<% end %>
/app/views/inquiry/confirm.html.erb
<div class="page-header">
  <h1>お問い合わせ</h1>
</div>

<%= form_for @inquiry, :url => inquiry_thanks_path do |f| %>
  <table class="table">
    <tr>
      <th>名前</th>
      <td>
        <%= f.hidden_field :name %>
        <%= @inquiry.name %>
      </td>
    </tr>
    <tr>
      <th>メールアドレス</th>
      <td>
        <%= f.hidden_field :email %>
        <%= @inquiry.email %>
      </td>
    </tr>
    <tr>
      <th>お問い合わせ内容</th>
      <td>
        <%= f.hidden_field :message %>
        <%= simple_format(@inquiry.message) %>
      </td>
    </tr>
  </table>
  <%= f.submit '送信', :class => 'btn btn-primary' %>
<% end %>
/app/views/inquiry/thanks.html.erb
<div class="page-header">
  <h1>お問い合わせ</h1>
</div>
<p>
  お問い合わせいただきありがとうございました。
</p>

4.models

$ rails g model inquiry
/app/models/inquiry.rb
class Inquiry
  include ActiveModel::Model

  attr_accessor :name, :email, :message

  validates :name, :presence => {:message => '名前を入力してください'}
  validates :email, :presence => {:message => 'メールアドレスを入力してください'}
end

5.mailers

$ rails g mailer inquiry_mailer
/app/mailers/inquiry_mailer.rb
class InquiryMailer < ActionMailer::Base
  default from: "example@example.com"   # 送信元アドレス
  default to: "example@example.com"     # 送信先アドレス

  def received_email(inquiry)
    @inquiry = inquiry
    mail(:subject => 'お問い合わせを承りました')
  end

end
/app/views/inquiry_mailer/received_email.text.erb
Webサイトからお問い合わせがありました。

--------------------------
Name: <%= @inquiry.name %>
Email: <%= @inquiry.email %>
Message:
<%= @inquiry.message %>
--------------------------
$ bundle exec rake db:migrate
6
4
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
6
4