首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 5入门教程,No route matches [POST]“/POST/new”,

Rails 5入门教程,No route matches [POST]“/POST/new”,
EN

Stack Overflow用户
提问于 2016-08-30 07:38:01
回答 2查看 710关注 0票数 0

我正在学习本教程: Rails http://guides.rubyonrails.org/getting_started.html入门

我在堆栈上进行了搜索:没有与发布“/ No route matches [POST] "/articles/new" /No route matches [POST] "/articles/new"”匹配的路由,推荐的拼写更正对我的错误没有帮助。

你可以找到我的git:https://github.com/tomile/rails5Blog/tree/adding-partial

routes.rb

代码语言:javascript
复制
Rails.application.routes.draw do
  resources :articles
  get 'welcome/index'
  root 'welcome#index'
end

articles_controller.rb

代码语言:javascript
复制
def create
    @article = Article.new(article_params)

    if @article.save
        redirect_to @article
    else
        render 'new'
    end
end

_form.html.erb (未复制粘贴整个文件)

代码语言:javascript
复制
<%= form_for :article do |f| %>

..。

代码语言:javascript
复制
  <p>
    <%= f.submit %>
  </p>
<% end %>

new.html.erb

代码语言:javascript
复制
<h1>New Article</h1>

<%= render 'form' %>

<%= link_to 'Back', articles_path %>

$rake路由

代码语言:javascript
复制
           Prefix Verb   URI Pattern                  Controller#Action
         articles GET    /articles(.:format)          articles#index
                  POST   /articles(.:format)          articles#create
      new_article GET    /articles/new(.:format)      articles#new
                  POST   /articles(.:format)          articles#create
      new_article GET    /articles/new(.:format)      articles#new
     edit_article GET    /articles/:id/edit(.:format) articles#edit
          article GET    /articles/:id(.:format)      articles#show
                  PATCH  /articles/:id(.:format)      articles#update
                  PUT    /articles/:id(.:format)      articles#update
                  DELETE /articles/:id(.:format)      articles#destroy
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-30 07:42:55

首先,新路由的类型是GET。第二件事是,当我们使用form_for时,我们为它提供了一个实例。

所以在你的文章中,控制器

代码语言:javascript
复制
def new
  @article = Article.new
end

然后在表单中使用

代码语言:javascript
复制
<%= form_for @article do |f| %>

它将指向创建文章的方法的表单动作。

票数 1
EN

Stack Overflow用户

发布于 2017-09-02 21:04:36

您遗漏了教程中的一个步骤。您必须将表单的第一行更改为以下内容:

代码语言:javascript
复制
<%= form_for :article, url: articles_path do |f| %>

之后,表单就可以正常工作了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39216752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档