![](/img/trans.png)
[英]Rails: Form_for: No route matches {:action=>“show”, :controller=>“posts”} missing required keys: [:id]
[英]No route matches {:action=>"comment", :id=>"1", :controller=>"posts"} on rails
<%= form_tag :action => "comment" do %>
<%= text_area "comment", "body" %><br />
<%= submit_tag "Comment" %>
<% end %>
我收到此錯誤:
no route matches found {:action=>"comment", :id=>"1", :controller=>"posts"
我該如何解決這個問題?
為了訪問{:action=>"comment", :id=>"1", :controller=>"posts"}
路由
在routes.rb
,您需要指定如下路線:
post "posts/:id/comment" => "posts#comment"
此外,您可以運行rake routes
以檢查應用程序的可用路由。
從您的問題中不太清楚,但我想您的post_controller
有一個comment
方法。 並且您想發布到該方法以創建新評論。
你會在你的config/routes.rb
這樣的東西:
resources :posts
您需要將其更改為:
resources :posts do
post 'comment', on: :member
end
關於路由如何工作的好書: http : //guides.rubyonrails.org/routing.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.