Noroute matches [GET]의 원인과 해결 방법

1681 단어 RubyRails

잘못


post/index 브라우저에서post 컨트롤러의destroy 동작을 실시한 결과 다음과 같은 오류가 발생했습니다.

잘못된 곳

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

해결책


일단 라크 루트로 확인을 해볼게요.
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

해결책


루스랑 방법을 바꿔서 해결했어.

① POST를 DELETE로 변경


routes에서 POST/posts/: id/destroy (.:format) 가DELETE/posts/: id/destroy (.:format) 로 변경됨
참고 자료: https://railsguides.jp/routing.html

②link_button toto로 변경

<%= button_to("削除", "/posts/#{@post.id}/destroy",method: :delete) %>
참고 자료: https://qiita.com/y-temp4/items/2d50feb3ff0d65acdf67

메모지


link_to부터 button.to로 변경된 후 View는 이렇게 됩니다.

좋은 웹페이지 즐겨찾기