Search code examples
ruby-on-railscontrollerruby-on-rails-5mail-form

Setting up a simple Rails 5 Mailform


It's been a while since I've programmed in Rails ... getting up to date with all the Rails 5.0 syntax and changes.

Using Rails 5.0.0.1

Using Ruby ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin16]

I'm trying to setup a simple contact us form on a landing page. I'm going the route of sending the email direct from the form vs. storing it to the database.

I'm using the mail_form gem and following this thread

I know I'm making some rookie mistakes on my controllers but after following several Stack Q/A's I'm still not quite there.

The model is successfully sending email in Rails Console. I just can't get the controller working. This is a one page site so I'm adding partials to the Index page in the Pages View folder.

Error I'm getting

AbstractController::ActionNotFound (The action 'create' could not be found for PagesController):

Routes

 Rails.application.routes.draw do
  get 'users/new'
  resources :pages
  root 'pages#index'
end

Form Partial

app/views/pages/_form.html.erb

<%= form_tag(pages_path)  do %>
    <div class="row">
        <div class="column width-6">
            <%= text_field_tag 'firstname', nil, class: 'form-element rounded large', placeholder: 'First Name*', tabindex: '1' %>
        </div>
        <div class="column width-6">
            <%= text_field_tag 'lastname', nil, class: 'form-element rounded large', placeholder: 'Last Name*', tabindex: '2' %>
        </div>
        <div class="column width-6">
            <%= email_field_tag 'email', nil, class: 'form-element rounded large', placeholder: 'Email Address*', tabindex: '3' %>
        </div>
        <div class="column width-6">
            <%= text_field_tag 'website', nil, class: 'form-element rounded large', placeholder: 'Website', tabindex: '4' %>
        </div>
        <div class="column width-6">
            <%= text_field_tag 'phone', nil, class: 'form-element rounded large', placeholder: 'Phone', tabindex: '5' %>
        </div>
    </div>
    <div class="row">
        <div class="column width-12">
            <%= text_area_tag 'message', nil, class: 'form-element rounded large', placeholder: 'Message*', tabindex: '6' %>
        </div>
        <div class="column width-12">
            <%= submit_tag 'Send Email', class: 'form-submit button rounded medium bkg-theme bkg-hover-green color-white color-hover-white'  %>
        </div>
    </div>
<% end %>

Pages Controller

class PagesController < ApplicationController
  def index
    @contact = Page.new(params[:page])
    if @contact.deliver
      redirect_to :back, :notice => "Thank you for contacting us, We'll get back to you shortly!"
    else
      flash.now[:error] = 'Sorry, it looks like there was an error with your message, Please give us a call or shoot us a text at ....'
    end
  end
end

Thanks for the help. This community is amazing!


Solution

  • Your routes are missing for the pages controller.

    in config/routes.rb add:

    resources :pages

    in PagesController.rb

    class PagesController < ApplicationController
      def create
        @contact = Page.new(params[:page])
        if @contact.deliver
          redirect_to :back, :notice => "Thank you for contacting us, We'll get back to you shortly!"
        else
          flash.now[:error] = 'Sorry, it looks like there was an error with your message, Please give us a call or shoot us a text at ....'
        end
      end
    end
    

    which handles AJAX posts.