0

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!

Community
  • 1
  • 1
DaveG
  • 1,203
  • 1
  • 25
  • 45

2 Answers2

0

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.

Blair Anderson
  • 19,463
  • 8
  • 77
  • 114
  • I changed the routes...but then got the following error...AbstractController::ActionNotFound (The action 'create' could not be found for PagesController): – DaveG Sep 27 '16 at 02:49
  • 1
    you're learning rails, keep going. i would really recommend reading http://guides.rubyonrails.org/routing.html - in your terminal run `rake routes` and you'll see that 7 routes were created and your form is pointing to the `create` and not the `index`... change the name of your method in the controller from `index` to `create` and it will work. – Blair Anderson Sep 27 '16 at 02:52
0

redirect_to :back is deprecated in rails 5. Instead there is a new function called redirect_back.

But I wouldn't use the index action for creating a new Page, even if you don't save it to the database. Instead I would define a new action called create and redirect to index in the end. As you already use resources :pages in the routes, you don't need to add anything there. Here you find the default routes and their actions, and what they should be used for: http://edgeguides.rubyonrails.org/routing.html#resource-routing-the-rails-default

Also I would consider using form_for instead of form_tag if you're working with a model. Here you find a simple example: http://edgeguides.rubyonrails.org/getting_started.html#the-first-form

I hope this helped a little :)

  • Thanks Zora. This is a simple marketing page so I was trying to avoid setting up a database and thereby avoided form_for...seems overkill. – DaveG Sep 27 '16 at 19:50