Rails:找不到ContactsController的动作'index'

时间:2015-11-19 05:40:52

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我正在构建一个Rails应用程序的联系表单,并收到以下错误:

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

我很困惑,因为我之前已经建立了联系表格,同样的方式也毫无问题。这看起来很奇怪,因为我根本没有在ContactsController中使用或引用index.html.erb或index方法?请找到以下文件:

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

模型/ 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

寄件人/ 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

视图/布局/ 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>

视图/联系人/ 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'

日志:

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://runpixelrun-projects-runpixelrun.c9users.io:8080/contacts/new
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)

它看起来像发送和被称为好吗?

由于

贾斯汀

1 个答案:

答案 0 :(得分:4)

如果您正在使用资源。您应该在控制器中执行所有这些操作。

GET /photos photos#index    display a list of all photos
GET /photos/new photos#new  return an HTML form for creating a new photo
POST    /photos photos#create   create a new photo
GET /photos/:id photos#show display a specific photo
GET /photos/:id/edit    photos#edit return an HTML form for editing a photo
PATCH/PUT   /photos/:id photos#update   update a specific photo
DELETE  /photos/:id photos#destroy  delete a specific photo

而不是路线中的resources :contacts。使用
resources :contacts, only: [:create,:new]

resources :contacts, except: [:index]

在上面指定了我给出的示例路由中所需的方法。 在resources :contacts, only: [:create,:new]中的第一个中,您的控制器中应该只有两个动作。

GET /photos/new photos#new  return an HTML form for creating a new photo
POST /photos    photos#create   create a new photo

如果您不想限制路线。请在控制器中指定操作: 对于你的控制器中的index动作就像这样。这也很有效。

def index
end