【发布时间】:2016-06-04 09:37:47
【问题描述】:
我需要一个帮助。使用 ROR 发布表单数据时出现以下错误。
No route matches [POST] "/articles/new"
Rails.root: C:/Sites/blog
Application Trace | Framework Trace | Full Trace
Routes
Routes match in priority from top to bottom
Helper HTTP Verb Path Controller#Action
Path / Url
articles_path GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article_path GET /articles/new(.:format) articles#new
edit_article_path GET /articles/:id/edit(.:format) articles#edit
article_path GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#up
我在下面解释我的代码。
new.html.erb:
<%= form_for @article do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
date
当用户提交此表单时,会出现此类错误。我的route.rb 文件如下所示。
Rails.application.routes.draw do
resources :articles
root 'welcome#index'
end
我的控制器文件代码如下。
class ArticlesController < ApplicationController
def new
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
end
我使用的是 Rails 版本4.2...。请帮我解决这个错误。
【问题讨论】:
-
Rails 默认的
new操作是GET /articles/new,但默认的create操作是POST /articles(最后没有/new);您的表单被提交到错误的 URL。顺便说一句,你的def new控制器方法不应该包含@article = Article.new吗? -
你能编辑你的答案吗?
标签: ruby forms ruby-on-rails-4