我正在学习本教程: 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
Rails.application.routes.draw do
resources :articles
get 'welcome/index'
root 'welcome#index'
endarticles_controller.rb
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end_form.html.erb (未复制粘贴整个文件)
<%= form_for :article do |f| %>..。
<p>
<%= f.submit %>
</p>
<% end %>new.html.erb
<h1>New Article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %>$rake路由
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发布于 2016-08-30 07:42:55
首先,新路由的类型是GET。第二件事是,当我们使用form_for时,我们为它提供了一个实例。
所以在你的文章中,控制器
def new
@article = Article.new
end然后在表单中使用
<%= form_for @article do |f| %>它将指向创建文章的方法的表单动作。
发布于 2017-09-02 21:04:36
您遗漏了教程中的一个步骤。您必须将表单的第一行更改为以下内容:
<%= form_for :article, url: articles_path do |f| %>之后,表单就可以正常工作了。
https://stackoverflow.com/questions/39216752
复制相似问题