#エラーメッセージ
Routing Error
No route matches [GET] "/blog/2/destroy"
#やりたいこと
idが2の@postを削除したいです。
postsコントローラーのdestroyアクションに繋がるルーティングはPOSTにしています。
link_to メソッドでルーティングをpostに指定しましたが、うまくいきません。
間違っている箇所を教えていただけるとありがたいです。
#書いているコード
routes
1 get 'contacts/new' 2 get 'contacts/create' 3 4 get 'blog' => "posts#index" 5 get 'blog/new' => "posts#new" 6 post 'blog' =>"posts#create" 7 get 'blog/:id' => "posts#show" 8 post 'blog/:id/destroy' => "posts#destroy" 9 get 'blog/:id/edit' => "posts#edit" 10 post 'blog/:id/update' => "posts#update" 11 12 get 'access' => "top#access" 13 get 'lesson' => "lesson#index" 14 get 'profile' => "top#profile" 15 get 'users/show' 16 17 root 'top#index'
PostsController
1class PostsController < ApplicationController 2 def index 3 @posts = Post.all 4 end 5 6 def show 7 @id = params[:id] 8 @post = Post.find_by(id: params[:id]) 9 end 10 11 def new 12 end 13 14 def create 15 params[:title] 16 params[:content] 17 @post = Post.new(title: params[:title], content: params[:content]) 18 @post.save 19 redirect_to("/blog") 20 end 21 22 def edit 23 @post = Post.find_by(id: params[:id]) 24 end 25 26 def update 27 @post = Post.find_by(id: params[:id]) 28 @post.title = params[:title] 29 @post.content = params[:content] 30 @post.save 31 redirect_to("/blog") 32 end 33 34 def destroy 35 @post =Post.find_by(id: params[:id]) 36 @post.destroy 37 redirect_to("/blog") 38 end 39end
html
1<%= form_tag({contoller: :posts, action: :update}, method: :post) do %> 2 <div class="new-post-submit"> 3 <textarea name="title" rows="1" cols="80"><%= @post.title %></textarea> 4 <textarea name="content" rows="8" cols="80"><%= @post.content %></textarea> 5 <input type="submit" value="edit"> 6 </div> 7<% end %> 8<%= link_to "削除", "/blog/#{@post.id}/destroy" method: :post%>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/05 23:58
2019/09/06 00:42
2019/09/06 00:52 編集
2019/09/06 08:13
2019/09/06 12:38
2019/09/06 12:45
2019/09/06 13:29
2019/09/06 14:03
2019/09/07 13:00
2019/09/07 13:45
2019/09/08 16:51