Rails 帮助程序 form_for() 忽略 url 参数



收到错误:ActionController::RoutingError (No route matches [POST] "/articles/new"):

从页面进行开机自检时/articles/new

我在/app/views/articles/new.html.erb内的 erb 代码:

<%= form_for :article, url: articles_path 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>

/bin/rake routes输出:

Running via Spring preloader in process 1067
       Prefix Verb   URI Pattern                  Controller#Action
welcome_index GET    /welcome/index(.:format)     welcome#index
         root GET    /                            welcome#index
     articles GET    /articles(.:format)          articles#index
              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

只需将其更改为:

<%= form_for Article.new do |f| %>

在 Ruby 文档中阅读有关如何将资源与 form_for一起使用的信息。

在我尝试引发异常并且它不起作用后,我变得可疑。 然后我意识到带有表单的页面从未在浏览器中刷新过。我只是按下并重新提交表格。愚蠢的错误。

始终执行硬刷新(无浏览器缓存)

刷新!!

最新更新