【问题标题】:No route matches {:action=>"comment", :id=>"1", :controller=>"posts"} on rails没有路线匹配 {:action=>"comment", :id=>"1", :controller=>"posts"} 在 rails
【发布时间】:2014-05-08 13:37:01
【问题描述】:
<%= form_tag :action => "comment" do %>     
  <%= text_area "comment", "body" %><br /> 
  <%= submit_tag "Comment" %> 
<% end %>

我收到了这个错误:

no route matches found  {:action=>"comment", :id=>"1", :controller=>"posts"

我该如何解决这个问题?

【问题讨论】:

  • $ rake routes$ bundle exec rake routes 将为您提供应用程序中的路线

标签: ruby-on-rails ruby


【解决方案1】:

为了访问{:action=&gt;"comment", :id=&gt;"1", :controller=&gt;"posts"}的路由

routes.rb 中,您需要指定如下路线:

post "posts/:id/comment" => "posts#comment"

此外,您可以运行 rake routes 以检查您的应用程序的可用路由。

【讨论】:

    【解决方案2】:

    你的问题不是很清楚,但我猜你的post_controller 中有一个comment 方法。并且您想发布到该方法以创建新评论。

    你会在你的config/routes.rb

    resources :posts
    

    你需要把它改成:

    resources :posts do
      post 'comment', on: :member
    end
    

    很好地了解路由的工作原理:http://guides.rubyonrails.org/routing.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多