LoginSignup
18
14

More than 5 years have passed since last update.

No route matches [GET]の原因と解決策

Last updated at Posted at 2019-02-26

エラー

post/indexのブラウザ上で、postコントローラーのdestroyアクションを実施した結果、以下のエラーが起きた。

image.png

エラーの該当箇所

<%= link_to("削除", "/posts/#{@post.id}/destroy", {method: "delete"}) %>

解決策

rake routesでひとまず確認

rake routes
 posts_create POST   /posts/create(.:format)           posts#create
               GET    /posts/:id/edit(.:format)         posts#edit
               POST   /posts/:id/update(.:format)       posts#update
               POST   /posts/:id/destroy(.:format)      posts#destroy

解決策

routesと、メソッドを変えたら解決した。

①POSTをDELETEに変更

routesで、POST /posts/:id/destroy(.:format) を、DELETE /posts/:id/destroy(.:format)に変更

参考記事:https://railsguides.jp/routing.html

②link_toをbutton_toに変更

<%= button_to("削除", "/posts/#{@post.id}/destroy",method: :delete) %>

参考記事:https://qiita.com/y-temp4/items/2d50feb3ff0d65acdf67

メモ

link_toからbutton_toに変更すると、Viewはこうなった。

image.png

18
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
14