前提・実現したいこと
Railsで簡単なblogAppの写経中にeditからupadateに送信時にrouting errorが起こりました。
https://ruby-rails.hatenadiary.com/entry/20140813/1407915718
発生している問題・エラーメッセージ
Routing Error No route matches [PATCH] "/posts.1" Rails.root: /Users/xxxxx/workz/blog Application Trace | Framework Trace | Full Trace Routes Routes match in priority from top to bottom
該当のソースコード
#####routes.rb
ruby
1Rails.application.routes.draw do 2 3 resources :posts 4 5 root 'welcome#index' 6 7end
#####rake routes
Prefix Verb URI Pattern Controller#Action posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PATCH /posts/:id(.:format) posts#update PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy root GET / welcome#index rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
#####posts_controller.rb
class PostsController < ApplicationController def index @posts = Post.all end def new @post = Post.new end def show @post = Post.find(params[:id]) end def edit @post = Post.find(params[:id]) end def create @post = Post.new(post_params) if @post.save redirect_to @post else render :new, status: :unprocessable_entity end end def update @post = Post.find(params[:id]) if @post.update(post_params) redirect_to @post else render :edit, status: :unprocessable_entity end end def destroy end private def post_params params.require(:post).permit(:title, :text) end end
#####edit.html.erb
<h1>投稿を更新</h1> <%= form_for :post, url: posts_path, method: :patch do |f| %> <% if @post.errors.any? %> <div id="error_explanation"> <h2><%= @post.errors.count %>件のエラーが発生したため保存ができませんでした。</h2> <ul> <% @post.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <p> <%= f.label :title, "タイトル" %><br> <%= f.text_field :title %> </p> <p> <%= f.label :text, "内容" %><br> <%= f.text_area :text, cols: 60, rows: 8 %> </p> <p> <%= f.submit %> </p> <% end %> <%= link_to '戻る', posts_path %>
試したこと
edit.html.erb
でresourcesのurlをposts_path(@post)
に変えたりもしましたがだめでした。
routesを読んだ感じだと'patch'の指定もしてるしルーティングも'resources'でしているのでどこを変えればいいのか全然わかりません。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/16 14:31