mercredi 18 novembre 2015

Rails: The action 'index' could not be found for ContactsController

I'm building out a contact form for a Rails App and am getting the following error:

'The action 'index' could not be found for ContactsController'

I'm confused because I've built contact forms before, the same way and with no problem. It seems odd because I'm not using or referencing an index.html.erb or index method in my ContactsController at all? Please find the files below:

contacts_controller.rb

  class ContactsController < ApplicationController

        def new
            @contact = Contact.new
        end

        def create
            @contact = Contact.new(contact_params)

            if @contact.save
                flash[:success] = "Message Sent!"
                redirect_to new_contact_path
            else
                flash[:danger] = "Error occurred"
                redirect_to new_contact_path
            end
        end

        private
            def contact_params
                params.require(:contact).permit(:name, :phone, :email, :comments)
            end
    end

models/contact.rb

class Contact < ActiveRecord::Base
    validates :name, presence: true
    validates :email, presence: true

    after_create :send_email

    private
        def send_email
            ContactMailer.contact_email(self).deliver
        end
end

mailers/contact_mailer.rb

class ContactMailer < ActionMailer::Base
    default to: 'example@example.com.au'

    def contact_email(contact)
        @contact = contact

        mail(from: @contact.email, subject: 'Contact Form Message').deliver
    end

end

views/layouts/application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>RunpixelrunWebsite</title>
  <%= stylesheet_link_tag    'application', media: 'all' %>
  <%= javascript_include_tag 'application' %>
  <%= csrf_meta_tags %>
</head>
<body>
  <div class="container">

    <% flash.each do |key, value| %>
    <div class="alert alert-<%= key %> alert-dismissable">
      <button type="button" class ="close" data-dismiss="alert" aria-label="Close"><span aria-hidden = "true">&times;</span></button>
      <%= value %>
    </div>
  <% end %>


<%= yield %>
  </div>

</body>
</html>

views/contacts/new.html.erb

<div class="row"> 
    <div class="col-md-4 col-md-offset-4">
        <div class="well">
            <%= form_for @contact do |f| %>
                <div class="form-group">
                    <%= f.label :name %>
                    <%= f.text_field :name, class: 'form-control' %>
                </div>

                <div class="form-group">
                    <%= f.label :phone %>
                    <%= f.text_field :phone, class: 'form-control' %>
                </div>

                <div class="form-group">
                    <%= f.label :email %>
                    <%= f.email_field :email, class: 'form-control' %>
                </div>

                <div class="form-group">
                    <%= f.label :comments %>
                    <%= f.text_area :comments, class: 'form-control' %>
                </div>

                <%= f.submit "Submit", class: "btn btn-default" %>
            <% end %>
        </div>
    </div>
</div>

routes.rb

Rails.application.routes.draw do

  resources :contacts

  root 'contacts#new'

log:

Started POST "/contacts" for 10.240.0.44 at 2015-11-19 05:25:05 +0000
Cannot render console from 10.240.0.44! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ContactsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ruEgewytZuJBEBh2bHoJJWj9uY4dp69JQOVFyl/V+v9l+dBTi3ZQn1Iq8Mb4D6bUSLDAvG9dNMSCKLVrfsDAKw==", "contact"=>{"name"=>"asdf", "phone"=>"asdf", "email"=>"asdfadsf@sfdgasd", "comments"=>"asdfasdf"}, "commit"=>"Submit"}
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "contacts" ("name", "phone", "email", "comments", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["name", "asdf"], ["phone", "asdf"], ["email", "asdfadsf@sfdgasd"], ["comments", "asdfasdf"], ["created_at", "2015-11-19 05:25:05.377425"], ["updated_at", "2015-11-19 05:25:05.377425"]]
DEPRECATION WARNING: `#deliver` is deprecated and will be removed in Rails 5. Use `#deliver_now` to deliver immediately or `#deliver_later` to deliver through Active Job. (called from send_email at /home/ubuntu/workspace/runpixelrun_website/app/models/contact.rb:9)
  Rendered contact_mailer/contact_email.html.erb (1.6ms)

Sent mail to example@example.com.au (30014.0ms)
Date: Thu, 19 Nov 2015 05:25:05 +0000
From: asdfadsf@sfdgasd
To: example@example.com.au
Message-ID: <564d5d319e2ca_56b22a3201c112db@runpixelrun-runpixelrun_projects-2170203.mail>
Subject: Contact Form Message
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE Html>
<html>
    <head>
        <title></title>
    </head>
    <body>

        <p>New message from RuNpiXelruN's Contact Form!, from asdf, asdfadsf@sfdgasd</p>
        </br>
        <p>asdf</p>
        <p>asdf</p>
        <p>asdfasdf</p>

    </body>
</html>

ContactMailer#contact_email: processed outbound mail in 30263.5ms

Sent mail to example@example.com.au (30006.4ms)
Date: Thu, 19 Nov 2015 05:25:05 +0000
From: asdfadsf@sfdgasd
To: example@example.com.au
Message-ID: <564d5d319e2ca_56b22a3201c112db@runpixelrun-runpixelrun_projects-2170203.mail>
Subject: Contact Form Message
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE Html>
<html>
    <head>
        <title></title>
    </head>
    <body>

        <p>New message from RuNpiXelruN's Contact Form!, from asdf, asdfadsf@sfdgasd</p>
        </br>
        <p>asdf</p>
        <p>asdf</p>
        <p>asdfasdf</p>

    </body>
</html>
   (18.5ms)  commit transaction
Redirected to http://ift.tt/1X04hU9
Completed 302 Found in 60313ms (ActiveRecord: 19.0ms)


Started GET "/contacts" for 10.240.0.45 at 2015-11-19 05:28:48 +0000
Cannot render console from 10.240.0.45! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

AbstractController::ActionNotFound (The action 'index' could not be found for ContactsController):
  actionpack (4.2.4) lib/abstract_controller/base.rb:132:in `process'
  actionview (4.2.4) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.4) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.2.4) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.4) lib/action_controller/metal.rb:237:in `block in action'
  actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:76:in `call'
  actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
  actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:45:in `serve'
  actionpack (4.2.4) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `each'
  actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:821:in `call'
  rack (1.6.4) lib/rack/etag.rb:24:in `call'
  rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.4) lib/rack/head.rb:13:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.4) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
  activerecord (4.2.4) lib/active_record/migration.rb:377:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.4) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
  activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
  activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  web-console (2.2.1) lib/web_console/middleware.rb:31:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.4) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.4) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.4) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.4) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/static.rb:116:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.4) lib/rails/engine.rb:518:in `call'
  railties (4.2.4) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
  /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
  /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'


  Rendered /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb within rescues/layout (0.6ms)

It all looks like it's sending and being called ok?

Thanks

Justin

Aucun commentaire:

Enregistrer un commentaire