ruby-on-rails – 路由错误 – 没有路由匹配[POST]“/ posts / new”

我正在浏览 rubyonrails.org的“博客教程”,当我尝试提交“帖子”时出现此错误:路由错误 – 没有路由匹配[POST]“/ posts / new”

我将教程中的代码复制并粘贴到我的代码中.这应该返回一个带有文章和帖子标题的哈希值,但是我得到了上面的错误.

这是我的观点:

<%= 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 %>

这是我的控制器:

class PostsController < ApplicationController
    def new
    end

    def create
        render text: params[:post].inspect
    end
end

这是我的routes.rb:

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

rake路线给出了:

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

以下是rails s窗口生成内容

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)

我在其他教程中遇到了同样的错误,我试图遵循verbatum.我错过了什么?

谢谢.

解决方法

我相信您的问题可能出在以下方面:
<%= form_for :post,url: posts_path do |f| %>

将其更改为:

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

我遇到了同样的问题. / posts / new正在加载,但是当我提交表单时,我收到了路由错误.

通过在/ views / posts文件夹中有一个名为_form.html.erb的单独文件,您应该为新操作和编辑操作使用相同的表单.

然后,在新视图和编辑视图中,您将引用此表单:

<%= render "form" %>

经过多次初步混淆后,这对我有用.

祝好运!

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...