Routing error - no route matches [POST] "/ posts / new"

I am working on a rubyonrails.org blog tutorial and get this error when I try to send a message: Routing error - No match routes [POST] "/ posts / new"

I copied and pasted the code from the tutorial into my code. This should return a hash with the text and message header, 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 are my .rb routes:

 Blog::Application.routes.draw do resources :posts end 

rake routes give the following:

  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 created the rails window:

 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 got the same error with other textbooks that I tried to complete the afterword. What am I missing?

Thanks.

+9
source share
8 answers

I believe your problem might be on this line:

 <%= form_for :post, url: posts_path do |f| %> 

Change it to:

 <%= form_for @post do |f| %> 

I had the same problem. /posts/new loaded, but when I submit the form, I got a routing error.

You must have the same form for the new and edit actions, having one separate file named _form.html.erb in the /views/posts folder.

Then in your new and edit views, you reference this form with:

 <%= render "form" %> 

It worked for me after a lot of confusion.

Good luck

+4
source

First, you can simply use <%= form_for @post do |f| %> <%= form_for @post do |f| %> .

Secondly, your controller must have a link to @post . For example, in new it will be @post = Post.new .

+2
source

Is it possible that you have a message model instead of a Post model - single and multiple? Check the model file name (post.rb vs. posts.rb) and the class name in this file (Post vs. Posts).

It seems that "posts_path" resolves to "posts / new" instead of "posts", since from your error message I can see that the POST request for "posts / new" is being sent, and submitting the form should result in a POST to '/ posts request '.

There may be other reasons why posts_path behaves this way, but my first guess is singular / plural.

Edit: Now I tried to reproduce the problem, but neither changing the model with respect to the singular / plural, nor deleting the model completely led to the corresponding behavior for me.

0
source

Try creating a model in a controller action:

Controller:

 def new @post = Post.new end 

View:

 <%= form_for @post do |f| %> 

Also post the generated html, please.

0
source

I am not sure what to fix. @titanboa - the tutorial has not yet considered generation models, so I continued to study the tutorial as if everything worked. After creating the model, it now works fine. Sorry for the unsatisfactory conclusion, but thanks for all the input!

0
source

I had the same problem until I found out that I need to update, not the back button, to go to a page / new page. Hope this helps.

0
source

I had the same problem and I found the answer to my problem in another thread.

Another thread on the same issue.

My problem was that I had a show show function under a private function in my posts_controller file. Everything below the private keyword is also private.

0
source

I had the same problem. I tried all the answers in similar posts, but nothing worked, including the ones above.

I even uninstalled the application, repeated the process and got stuck in the same problem.

After much disappointment, I realized that this is due to the text_area field. In each case, I copied arbitrary text from the web page into the body of the main text that caused the error. I knew this because I decided instead to type arbitrary text and voila! He worked for the pitch.

All this time there was nothing wrong. Since the Ruby attribute text_area has no restrictions , I think it should be due to the characters that I copied, confusing with the code in the background, as it converts what it does not understand into HTML, as indicated here.

This is my special case, and I thought I should share.

0
source

Source: https://habr.com/ru/post/955325/


All Articles