Mindaugas Bernatavičius
Mindaugas Bernatavičius

Reputation: 3909

Rails helper form_for() ignores the url parameter

Getting the error: ActionController::RoutingError (No route matches [POST] "/articles/new"):

When doing a POST from /articles/new page.

My erb code inside the /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 output:

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

Upvotes: 0

Views: 125

Answers (2)

Mindaugas Bernatavičius
Mindaugas Bernatavičius

Reputation: 3909

After I tried raising exceptions and it not working I became suspicious. then I realized that the page with the form was never refreshed in the browser. I was just pressing back and resubmitting the form. Dumb mistake.

ALWAYS DO HARD (no browser cache) REFRESHES!!!

Upvotes: 0

spickermann
spickermann

Reputation: 107077

Just change it to:

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

Read about how to use resources with form_for in the Ruby docs.

Upvotes: 1

Related Questions