使用Ruby on Rails提交后出现路由错误



每当我按下提交按钮时,错误显示为:No route matches [POST] "/contacts/new"我刚开始学习Ruby on Rails,我不知道如何解决这个问题。非常感谢任何帮助!谢谢:)


Rails.application.routes.draw do
root to: 'pages#home'
get 'about', to: 'pages#about'
resources :contacts
end
以上是我的路由文件
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contact_params)
if @contact.save
redirect_to new_contact_path, notice: "Message sent."
else
redirect_to new_contact_path, notice: "Error occured."
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
end

以上是我的控制器文件

<div class="container">
<div class="row">

<h3 class="text-center">Contact us</h3>

<div class="col-md-4 col-md-offset-4">
<%= flash[:notice] %>
<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 :email %>
<%= f.text_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>
</div>

上面是我的视图文件

class CreateContacts < ActiveRecord::Migration[5.0]
def change
create_table :contacts do |t|
t.string :name
t.string :email
t.text :comments
t.timestamps
end
end
end

以上是我的迁移文件

ActiveRecord::Schema.define(version: 20210210035931) do
create_table "contacts", force: :cascade do |t|
end
end

以上是我的schema。rb文件。我有一个问题:根据我所学到的,我会在模式中自动生成列(例如t.string "email")。Rb文件,但它在我的文件中显示为空。这正常吗?手动添加了列,但路由错误仍然存在。

class Contact < ActiveRecord::Base

end

以上是我的联系人模型文件(Contact .rb)

Started GET "/contacts/new" for 128.106.252.65 at 2021-02-17 12:25:05 +0000
Cannot render console from 128.106.252.65! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ContactsController#new as HTML
Rendering contacts/new.html.erb within layouts/application
Rendered contacts/new.html.erb within layouts/application (3.1ms)
Completed 200 OK in 54ms (Views: 52.4ms | ActiveRecord: 0.0ms)

Started POST "/contacts/new" for 128.106.252.65 at 2021-02-17 12:25:08 +0000
Cannot render console from 128.106.252.65! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

ActionController::RoutingError (No route matches [POST] "/contacts/new"):

actionpack (5.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'
web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.7.0) lib/web_console/middleware.rb:22:in `block in call'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.0) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.2.3) lib/rack/method_override.rb:24:in `call'
rack (2.2.3) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
railties (5.0.0) lib/rails/engine.rb:522:in `call'
puma (3.4.0) lib/puma/configuration.rb:224:in `call'
puma (3.4.0) lib/puma/server.rb:569:in `handle_request'
puma (3.4.0) lib/puma/server.rb:406:in `process_client'
puma (3.4.0) lib/puma/server.rb:271:in `block in run'
puma (3.4.0) lib/puma/thread_pool.rb:114:in `block in spawn_thread'
Rendering /home/ec2-user/.rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout
Rendering /home/ec2-user/.rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /home/ec2-user/.rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.4ms)
Rendered collection of /home/ec2-user/.rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/routes/_route.html.erb [10 times] (3.0ms)
Rendered /home/ec2-user/.rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/routes/_table.html.erb (0.9ms)
Rendering /home/ec2-user/.rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /home/ec2-user/.rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
Rendered /home/ec2-user/.rvm/gems/ruby-2.3.0@saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (27.1ms)

上面是完整的堆栈跟踪。

路由错误信息截图

当你提交一个表单给resource时,它应该提交给[POST] /contacts/,而不是错误中所说的[POST] /contacts/new

你应该更新你的表单以提交到正确的路径。

提示:在终端中使用rails routes命令查看应用程序中的所有路由。

最新更新