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

前端之家收集整理的这篇文章主要介绍了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" %>

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

祝好运!

原文链接:https://www.f2er.com/ruby/270329.html

猜你在找的Ruby相关文章