Routing Error -- No route matches [POST] "/posts/new"
Solution 1
I believe your issue may be with this line:
<%= form_for :post, url: posts_path do |f| %>
Change it to:
<%= form_for @post do |f| %>
I was having the same issue. The /posts/new
was loading, but when I submit the form, I got a routing error.
You should have the same form for new
and edit
actions, by having one separate file called _form.html.erb
in /views/posts
folder.
Then, in your new
and edit
views, you reference this form with:
<%= render "form" %>
This worked for me, after much initial confusion.
Good luck!
Solution 2
Firstly, you can just use <%= form_for @post do |f| %>
.
Secondly, your controller needs to have a reference to @post
. For example in new
, it would be @post = Post.new
.
user2053119
Updated on July 26, 2022Comments
-
user2053119 almost 2 years
I'm working through the rubyonrails.org 'blog tutorial', and get this error when I try to submit a 'post' : Routing Error --No route matches [POST] "/posts/new"
I copied and pasted the code from the tutorial into my code. This should return a hash with the text and title of the post, but instead I get the above error.
Here is my view:
<%= form_for :post, url: posts_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> <% end %>
Here is my controller:
class PostsController < ApplicationController def new end def create render text: params[:post].inspect end end
Here is my routes.rb:
Blog::Application.routes.draw do resources :posts end
rake routes gives this:
posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy
Here is what the rails s window generated:
Started POST "/posts/new" for 127.0.0.1 at 2013-10-05 21:17:52 -0400 ActionController::RoutingError (No route matches [POST] "/posts/new"): actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app' railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call' activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged' railties (3.2.13) lib/rails/rack/logger.rb:16:in `call' actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call' rack (1.4.5) lib/rack/methodoverride.rb:21:in `call' rack (1.4.5) lib/rack/runtime.rb:17:in `call' activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call' rack (1.4.5) lib/rack/lock.rb:15:in `call' actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call' railties (3.2.13) lib/rails/engine.rb:479:in `call' railties (3.2.13) lib/rails/application.rb:223:in `call' rack (1.4.5) lib/rack/content_length.rb:14:in `call' railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call' rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service' C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templat es/rescues/routing_error.erb within rescues/layout (1.0ms)
I've gotten this same error with other tutorials that I've tried to follow verbatum. What am I missing?
Thanks.
-
user2053119 over 10 years-Thanks for the suggestion, however I still get the same error. I feel like similar errors have occurred in other tutorials...can there be something wrong with how Rails is adding new routes?
-
Benjamin Tan Wei Hao over 10 yearsYour routes are correct. Do you have the
Post
class inpost.rb
? Have you run all the necessary migrations? -
user2053119 over 10 years-Honestly, I don't know about those. This is the beginning of the tutorial, and it hasn't covered migrations yet, and didn't mention making a Post class in post.rb. The above code is all the code the tutorial had.