【问题标题】:Routing Error after submitting using Ruby on Rails使用 Ruby on Rails 提交后出现路由错误
【发布时间】:2021-05-18 11:05:08
【问题描述】:

每当我按下提交按钮时,都会显示错误消息: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" 在 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, ::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

【问题讨论】:

  • 什么时候出现错误?
  • 每当我按下提交按钮时。我正在创建一个简单的表单,我可以在其中输入内容,单击提交后,它将保存在数据库中
  • 您使用的是哪个版本的 Rails?
  • 你可以试试form_for Contact.new 代替form_for @contact 吗?我想知道为什么form_for @contact 会生成错误的 POST 路径!
  • @SharvyAhmed 我正在使用 rails 5.0.0。 form_for Contact.new 不起作用,现在我收到此代码 &lt;%= f.text_field :name, class: 'form-control' %&gt; 的新错误 (NoMethodError),说明“# 的未定义方法‘名称’”。还有其他想法吗?

标签: ruby-on-rails ruby error-handling routes submit


【解决方案1】:

当您向资源提交表单时,应将其提交给[POST] /contacts/ 而不是[POST] /contacts/new,如错误中所述。

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

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

【讨论】:

  • 如何将表单的提交更新到正确的路径?比如我在哪里改?谢谢你的回复:)
  • 一种快速的方法是在视图文件&lt;%= form_for @contact, url: contacts_path do |f| %&gt; 中更新表单,我只是将它定向到正确的 url 而不是错误的。
  • 另外请提及您正在学习的教程。这对任何试图提供帮助的人都有帮助。我建议关注official one
  • 我正在关注这个网站上的基本 Web 开发人员课程:upskillcourses.com/courses
  • 你试过我的建议了吗?对你有用吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多