0
0

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 3 years have passed since last update.

link_to メソッドで destroy アクションをルーティングする

Posted at

開発環境

ruby 2.6.5
Ruby on Rails 5.2.5

方法

通常だと link_to メソッドは GET メソッドでルーティングしてまうので

<td><%= link_to "削除", "/blogs/#{blog.id}", method: "DELETE" %></td>

こんな感じで第3引数にメソッドを指定すればオッケー

また、 resources でルーティングした際に 
rails routes でルーティングを確認すると


      Prefix Verb   URI Pattern                                                                                       Controller#Action
                                   blogs GET    /blogs(.:format)                                                                                  blogs#index
                                         POST   /blogs(.:format)                                                                                  blogs#create
                                new_blog GET    /blogs/new(.:format)                                                                              blogs#new
                               edit_blog GET    /blogs/:id/edit(.:format)                                                                         blogs#edit
                                    blog GET    /blogs/:id(.:format)                                                                              blogs#show
                                         PATCH  /blogs/:id(.:format)                                                                              blogs#update
                                         PUT    /blogs/:id(.:format)                                                                              blogs#update
                                         DELETE /blogs/:id(.:format)                                                                              blogs#destroy
 

こんな感じで destroy アクションに対する prefix がない
これは

省力されてるだけらしい
だから ~path っていう書き方もできる

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?