前提・実現したいこと
railsで編集ページの更新しようとした際、Routing Errorがでる。
発生している問題・エラーメッセージ
Routing Error
No route matches [PATCH] "/posts/8/update"
Rails.root: /home/ec2-user/environment/pet
Application Trace | Framework Trace | Full Trace
Routes
Routes match in priority from top to bottom
Helper HTTP Verb Path Controller#Action
Path / Url
Path Match
GET /
posts#index
posts_new_path GET /posts/new(.:format)
posts#new
GET /:id(.:format)
posts#show
GET /posts/:id/edit(.:format)
posts#edit
POST /posts/:id/update(.:format)
posts#update
posts_create_path POST /posts/create(.:format)
posts#create
Request
Parameters:
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"7ZB25p82F8sO67At/pEWAObI+q3ifTwls1QphfIdLN2HGoglv+sIgsWf4e+JE3/KWnpDieBzG54cC7ohj2zpIg==",
"post"=>{"title"=>"miyake", "body"=>"cool is cool"},
"commit"=>"Update Post"}
Toggle session dump
Toggle env dump
Response
Headers:
None
該当のソースコード
ruby on rails
補足情報(FW/ツールのバージョンなど)
#edit.html.erb
<h1>投稿編集</h1> <%= form_for @post, url:"/posts/#{@post.id}/update" do |f| %> <p> <%=f.text_field :title %> <% if @post.errors.messages[:title].any? %> <span class="errors"> <%= @post.errors.messages[:title][0] %> </span> <% end %> </p> <p> <%=f.text_area :body %> <% if @post.errors.messages[:body].any? %> <span class="errors"> <%= @post.errors.messages[:body][0] %> </span> <% end %> </p> <p> <%= f.submit %> </p><% end %>
#routes.rb
Rails.application.routes.draw do
get "/" => "posts#index"
get "posts/new" => "posts#new"
get "/:id" => "posts#show"
get "posts/:id/edit" =>"posts#edit"
post "posts/:id/update" => "posts#update"
post "posts/create" => "posts#create"
end
#contoller.rb
class PostsController < ApplicationController
def index
@posts = Post.all.order(created_at:'desc')
end
def new @post = Post.new end def create @post = Post.new(post_params) if @post.save redirect_to "/" else render 'new' end end def show @post = Post.find(params[:id]) end def edit @post = Post.find(params[:id]) end def update @post = Post.find(params[:id]) if @post.update(post_params) redirect_to "/" else render 'edit' end end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/18 03:54