Question

Rails 3.2

I use the Mail_form gem (from plataformatec) to create a simple 'contact us' form for my website. When click on 'send' I get a routing error that says:

Routing Error
No route matches [POST] "/contactus"
Try running rake routes for more information on available routes.

I have a very simple setup, but I am new to Rails and am still getting the hang of it. I only want the form to send an email to a certain email address... nothing else. I understand the problem is in routes.rb but I have been fiddling with this for so long I just can't figure out what is wrong. I have never struggled with a Rails error so much. PLEASE HELP!

'Pages' Model: app/models/pages.rb

class Page < MailForm::Base
  attribute :name,          :validate => true
  attribute :email,         :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :page_title,    :validate => true
  attribute :page_body,     :validate => true

  def headers
    :subject => "#{page_title}",
    :to => "careers@example.com",
    :from => %("#{name}" <#{email}>)
  end
end

'Pages' Controller: app/controllers/pages_controller.rb

class PagesController < ApplicationController
    respond_to :html

    def index
    end

    def create
        page = Page.new(params[:contact_form])
        if page.deliver
          redirect_to contactus_path, :notice => 'Email has been sent.'
        else
          redirect_to contactus_path, :notice => 'Email could not be sent.'
        end
    end
end

Form Partial: app/views/pages/_form.html.erb

<%= simple_form_for :contact_form, url: contactus_path, method: :post do |f| %>

<div>
    <%= f.input :name %>
    <%= f.input :email, label: 'Email address' %>
    <%= f.input :page_title, label: 'Title' %>
    <%= f.input :page_body, label: 'Your message', as: :text %>
</div>

<div class="form-actions">
    <%= f.button :submit, label: 'Send', as: :text %>
</div>

View (called contactus): app/views/pages/contactus.html.erb

<body>
    <div>
        <h2 class="centeralign text-info">Contact Us</h2>
    </div>
    <div class="container centeralign">
            <%= render 'form' %>
    </div>
            <h2>We'd love to hear from you! </h2><br /><h4 class="muted">Send us a message and we'll get back to you as soon as possible</h4>
    </div>
</div>
</body>

Routes.rb

Example::Application.routes.draw do
  resources :pages
  root to: 'pages#index', as: :home
  get 'contactus', to: 'pages#contactus', as: :contactus
  get 'services', to: 'pages#services', as: :services
Was it helpful?

Solution

Your routes.rb file doesn't have a route for POST /contactus You've got a route for GET /contactus but no POST, so what rails is saying is correct.

Just add something like

post 'contactus', to: 'controller#action'

With whatever controller and action you need to call. Alternatively, if you're trying to call the create action in the pages controller, then your problem is that where you've added resources :pages to routes, you've actually create the route

post 'pages'

So in that case, I'd change your simple_form_for url to post to there instead. Try using

simple_form_for :contact_form, url: pages_path, method: :post do

instead. If pages_path doesn't work, then just run rake routes in a console and you'll see a list of all of the routes you have including their names. Then just pick the one you need for this :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top