gpt4 book ai didi

ruby-on-rails - rails : The action 'index' could not be found for ContactsController

转载 作者:数据小太阳 更新时间:2023-10-29 08:36:43 25 4
gpt4 key购买 nike

我正在为 Rails 应用构建联系表单,但出现以下错误:

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

我很困惑,因为我以前建立过联系表格,方式相同,没有问题。这似乎很奇怪,因为我根本没有在我的 ContactsController 中使用或引用 index.html.erb 或索引方法?请找到以下文件:

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

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>

路线.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)

这一切看起来都在发送和被调用,好吗?

谢谢

贾斯汀

最佳答案

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

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

在你的 route 代替 resources :contacts。使用
资源:联系人,仅:[:创建,:新]

资源:联系人,除了:[:index]

在上面的示例路由中指定您需要的方法。在 resources :contacts, only: [:create,:new] 中的第一个中,您的 Controller 中应该只有两个操作。

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

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

def index
end

关于ruby-on-rails - rails : The action 'index' could not be found for ContactsController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33796078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com