Routing Error — No route matches [POST] “/posts/ne

2019-06-16 15:21发布

问题:

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.

回答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!



回答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.



回答3:

Try to the create model in controller action:

Controller:

def new
  @post = Post.new
end

View:

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

Also post the generated html, please.



回答4:

I'm not sure what fixed it. @titanboa--the tutorial had not yet covered generating models, so I kept going on in the tutorial as if everything was working. After generating the model, it now seems to be working fine. Sorry for the unsatisfying conclusion, but thanks for all the input!



回答5:

Is it possible that you have a Posts model instead of a Post model - singular versus plural? Please check the model's file name (post.rb vs. posts.rb) and the class name in that file (Post vs. Posts).

It looks like 'posts_path' resolves to 'posts/new' instead of 'posts', since from your error message I see there is a POST request to 'posts/new' while submitting the form should result in a POST request to '/posts'.

Maybe there are other reasons why 'posts_path' behaves like that, but my first guess is the singular/plural.

Edit: Now I've tried to reproduce the issue, but neither changing the model with regards to singular/plural nor removing the model completely resulted in the respective behaviour for me.



回答6:

I had the same problem, until later I found out that I need to Refresh, not click Back button to get to the posts/new page. Hope it helps.



回答7:

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

Other thread on same issue.

My problem was that I had my def show function below the private function in my posts_controller file. Anything below private keyword are also private apparently.



回答8:

I had the very same issue. I tried all the answers on similar posts and nothing worked including the ones above.

I even deleted the app and repeated the process and got stuck on the same issue.

After much frustration, I realised it had to do with the text_area field. In each case, I had copied random text from a web page into the body text field which was throwing the error. I knew this because I decided to type random text instead and voila! It worked on submission.

There was nothing wrong all the while. Since the Ruby text_area attribute has no limit, I guess it has to be with the characters I was copying messing with the code in the background since it converts what it does not understand to HTML as stated here.

This is my own particular case and I thought I should share.