3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsの削除のルーティンがうまく行かない Unknown action The action 'show' could not be found for BlogsController

Last updated at Posted at 2019-10-12

削除機能を実装中、画面にこんなエラーが。
showのルーティングがうまくいっていません?
showはメソッド実装していないから出るかもしれいけど、もしかして削除のリクエストを送るときのリクエストのメソッドが違う?

Unknown action
The action 'show' could not be found for BlogsController
app/controllers/blogs_controller.rb

  def destroy
    @blog = Blog.find(params[:id])
    @blog.destroy
    redirect_to blogs_path
    flash[:success] = 'delete success'

  end
config/routes.rb
Rails.application.routes.draw do
  resources :blogs
end

たくさんぐぐる。
ルーティングはresourcesでおまかせにしているし、リクエストのメソッド指定も合っていそう。

app/views/blogs/index.html.slim
 = link_to '削除', blog_path(blog.id), method: :delete, class: 'btn btn-primary'

いろいろぐぐって、参考サイトにたどりつく。

app/assets/javascripts/application.js

//= require activestorage
//= require turbolinks
//= require jquery
//= require popper
//= require bootstrap
//= require_tree .
//= require jquery_ujs  // <- これを足す

おそらくjquery-railsを入れているとこういうことになるっぽい。

参考サイト

Rails 削除機能の実装方法 手順 - Qiita

forms - ruby on rails link_to delete method not working - Stack Overflow

3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?