Rails helper form_for()忽略url参数

时间:2016-06-11 13:14:03

标签: ruby-on-rails ruby

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

/articles/new页面执行POST时。

我在/app/views/articles/new.html.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

2 个答案:

答案 0 :(得分:1)

只需将其更改为:

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

了解如何在Ruby文档中使用resources with form_for

答案 1 :(得分:0)

在我尝试提出异常并且无法正常工作后,我开始怀疑。然后我意识到带有表单的页面从未在浏览器中刷新过。我只是按下并重新提交表格。愚蠢的错误。

总是很难(没有浏览器缓存)REFRESHES !!!