【Rails】【エラー】method: :deleteで指定しているのにshowアクションへ飛んでしまう
エラー発生
復習用に簡単なCRUD機能のついた記事投稿アプリを作成しています。
削除用のボタンを押しても削除されず、なぜかshow
詳細ページへ遷移してしまう…。
method: :delete
で指定し、Cromeの検証画面でも正常にみえるのに…
= link_to 'delete',post_path(post), class: "btn btn-danger", method: :delete, data: {confirm: 'Are you sure?' }
def destroy @post.destroy! flash[:success] = 'You deleted your post!' redirect_to posts_path end
resources :posts
原因
(略) //= require rails-ujs #これが抜けてた…
GitHub - rails/jquery-ujs: Ruby on Rails unobtrusive scripting adapter for jQuery
Unobtrusive scripting adapter for jQuery This unobtrusive scripting support file is developed for the Ruby on Rails framework, but is not strictly tied to any specific backend. You can drop this into any application to:
force confirmation dialogs for various actions; make non-GET requests from hyperlinks; make forms or hyperlinks submit data asynchronously with Ajax; have submit buttons become automatically disabled on form submit to prevent double-clicking. These features are achieved by adding certain "data" attributes to your HTML markup. In Rails, they are added by the framework's template helpers.
ハイパーリンクでGETリクエスト以外のものを生成してくれる。
軽く1時間は悩んでいたので解決してすっきり。
参考
[Rails 3] link_toのmethodが効かない!?|TechRacho(テックラッチョ)〜エンジニアの「?」を「!」に〜|BPS株式会社