没有路线匹配[POST]“/ articles / new”

时间:2014-05-26 07:27:53

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

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

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

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

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

这里的url:匹配作为创建的帖子请求

表格标题

<%= 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
控制器中的

8 个答案:

答案 0 :(得分:16)

许多人使用本教程的原因是,在更改 form_for 帮助程序中的 url 选项后​​,他们不会重新加载表单。

在尝试提交表单之前,请务必重新加载表单的新副本(您需要表单以获取最新的表单提交网址)。

答案 1 :(得分:5)

变化:

resources :article

为:

resources :articles #plural

它将映射到:

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

答案 2 :(得分:3)

我更改了app / views / articles / new.html.erb:

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

为:

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

答案 3 :(得分:2)

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

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| %>

答案 4 :(得分:1)

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

因为此路线转到您目前所处的页面,并且该路线仅应用于显示新文章的表单。

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

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

答案 5 :(得分:0)

RedZagogulin的回答是正确的 - 这是您需要在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

enter image description here

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

答案 6 :(得分:0)

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

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

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

<%= link_to "Blog", articles_path %>

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

答案 7 :(得分:0)

就你而言,
首先改变

#/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