【问题标题】:No route matches [POST] "/articles/new"没有路线匹配 [POST] "/articles/new"
【发布时间】:2014-05-26 07:27:53
【问题描述】:

当我尝试提交表单时,它给了我错误

No route matches [POST] "/articles/new"

文件是:new.html.erb
此文件包含带有文本字段和文本区域的表单:

<%= form_for :article, url: articles_path do |f| %>

这里的 url: 是为了匹配一个创建的 post 请求

表单的标题

<%= f.label :title %><br>

表单的文本字段

<%= f.text_field :title %></p>

表单的标题

<%= f.label :text %><br>

表单的文本区域

<%= f.text_area :text %></p>           
<p>
<%= f.submit %>
</p>
<% end %>

路由文件是

Rails.application.routes.draw do

resources :article
end

控制器是 控制器及其方法 new 和 create

每当我提交表单时都会出现错误,即使我使用了 URL:articles_path,默认的发布请求,我也在表单中使用了@articles,但它给了我同样的错误。我是 Rails 新手,所以我尝试了很多方法,但我找不到解决方案

class ArticlesController < ApplicationController

  def new #new method which is a get request
  end

  def create #create method which is a post request
  end
end

每当我提交表单时都会出现错误,即使我使用了 url:articles_path 作为默认的发布请求。我一直保持

def create
end

在控制器中

【问题讨论】:

  • 我添加资源的路由文件:articles

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


【解决方案1】:

许多使用本教程的人出现这种情况的原因是他们在更改 form_for 帮助程序中的 url 选项后没有重新加载表单。

请务必在尝试提交之前重新加载表单的新副本(您需要表单具有最新的表单提交 url)。

【讨论】:

    【解决方案2】:

    改变:

    resources :article
    

    到:

    resources :articles #plural
    

    这样它将映射到:

    articles_path    POST    /articles(.:format)     articles#create
    

    【讨论】:

    • 我改为资源 :articles 但它给了我同样的错误
    【解决方案3】:

    我将 app/views/articles/new.html.erb 从:

    &lt;%= form_for :article do |f| %&gt;

    到:

    &lt;%= form_for :article, url: articles_path do |f| %&gt;

    【讨论】:

      【解决方案4】:

      您可以在官方指南中找到答案。

      因为这条路线会转到您当前所在的页面,并且该路线应该只用于显示新文章的表单。

      编辑 app/views/articles/new.html.erb 中的 form_with 行,如下所示:

      <%= form_with scope: :article, url: articles_path, local: true do |form| %>
      

      【讨论】:

      • 但正如@Wibbly 上面回答的那样,更改 erb 代码后未能重新加载表单页面可能是人们绊倒的原因。这是教程中没有提到的关键步骤(重新加载)...
      【解决方案5】:

      您在控制器中的操作/方法什么都不做。它应该是这样的:

      class ArticlesController < ApplicationController
      
        def new 
          @article = Article.new
        end
      
        def create 
          @article = Article.new(article_params)
          if @article.save
            redirect_to @article
          else
            render 'new'
          end
        end
      
       private
      
        def article_params
          params.require(:article).permit()# here go you parameters for an article
        end
      
      end
      

      在视图中:

      <%= form_for @article do |f| %>
      

      【讨论】:

      • 我将上面的代码添加到我的应用程序中,但它仍然给我错误 No route matches [POST] "/articles/new"
      • 您是否在编辑完 config/routes.rb 后重新启动了 rails 服务器(按照 Wali Ali 回复您的方式)?
      【解决方案6】:

      RedZagogulins 的答案是正确的 - 这是您需要在 articles 控制器中使用的代码

      --

      路线

      你的问题的线索在这里:

      No route matches [POST] "/articles/new"
      

      通常,在使用 correct routing structure 时:

      #config/routes.rb
      resources :articles #-> needs to be controller name
      

      您会发现new 操作是GET,而不是POST。这让我相信你的系统设置不正确(它试图将表单数据发送到articles/new,而它应该只是发送到[POST] articles

      如果您按照RedZagogulin 概述的步骤操作,它应该适合您

      【讨论】:

        【解决方案7】:

        我一直遇到这个问题,归结为我的导航栏中有以下代码:

        <%= link_to "Blog", controller: 'articles' %>
        

        我切换到这个,一切都开始工作了:

        <%= link_to "Blog", articles_path %>
        

        我还是新手,所以不同的做事方式有时会让人感到困惑。

        【讨论】:

          【解决方案8】:

          在你的情况下,
          首先改变

          #/config/routes.rb
          
          resources :article
          

          到

          #/config/routes.rb
          
          resources :articles #plural
          
          # Changing this in Plural naming if your file name
          # in #/app/view
          # is plural
          # (ex: /app/view/articles)
          #
          # and controller and it's class is also in plural naming
          # (ex:
          # in #/app/controllers/articles_controller.rb file
          # class ArticlesController < ApplicationController
          #
          # end)
          
          

          在某些情况下,我们需要添加一个Post路由来制作一个Post Request的表单。只需在路由文件中添加以下行:

          #/config/routes.rb
          
          Rails.application.routes.draw do
          
            resources :articles #plural
            post '/articles/new' => 'articles#create'
          
          end
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-01-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多