嗨,我正在尝试为文章设置一个基本表单,但在提交表单时遇到以下错误:No route matches [POST] "/"
这是表格(我知道我可以使用form_for,但我正在尝试不使用它):
<form accept-charset="UTF-8" action="<% articles_path %>" method="post">
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
<input type="text" name="title" placeholder="title">
<input type="text" name="body" placeholder="body">
<input type="submit">
</form>
这是我的路线。rb:
Rails.application.routes.draw do
resource :articles, only: [:new, :create]
root to: 'articles#new'
end
以下是我的路线:
articles_path POST /articles(.:format) articles#create
new_articles_path GET /articles/new(.:format) articles#new
root_path GET / articles#new
我不明白的是,我的表单操作是带有post方法的articles_path,而应用程序正在寻找到根路由的post请求。它不应该进入我的文章路径POST路线吗?当我从articles/new提交表单时,也会发生同样的事情,只是它说articles/new没有路由POST。
我检查了其他类似的问题,答案总是与表单动作有关,据我所知,我是对的,所以我想我应该问自己的问题。
您在<% articles_path %>
中缺少一个=
。你的<form>
标签应该是:
<form accept-charset="UTF-8" action="<%= articles_path %>" method="post">
如果没有=
,HTML输出将包含action=""
,从而使表单发布到当前页面的URL。