如何解决使用 Ruby on Rails 提交后出现路由错误
每当我按下提交按钮时,错误都会显示:没有路由匹配 [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"
在 schema.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,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)
以上是完整的堆栈跟踪。
Routing Error Message screenshot
解决方法
当您向资源提交表单时,应将其提交给 [POST] /contacts/
而不是错误中所述的 [POST] /contacts/new
。
您应该更新表单以提交到正确的路径。
提示:在终端中使用 rails routes
命令查看应用程序中的所有路由。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。