前提
rails で投稿アプリを作っています。
実現したいこと
routing error の解決
発生している問題・エラーメッセージ
Routing Error No route matches [PATCH] "/posts"
該当のソースコード
routes.rb Rails.application.routes.draw do devise_for :users resources :users # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root :to => 'homes#top' resources :posts do resources :likes, only: [:create, :destroy] resources :comments, only: [:create, :destroy] end get 'post/rank' => 'posts#rank' get '/post/hashtag/:name' => 'posts#hashtag' get '/post/hashtag' => 'posts#hashtag' end
rails routes post GET /posts/:id(.:format) posts#show PATCH /posts/:id(.:format) posts#update PUT /posts/:id(.:format) posts#update
posts/controller def edit @post = Post.find(params[:id]) end def update post = Post.find(params[:id]) post.update(post_params) redirect_to post_path(post.id) end
view/edit <div class="post"> <h1>編集</h1> <div class="post-detail"> <%= form_with model: @post, url: '/posts', local:true do |f| %> <h4>タイトル</h4> <%= f.text_field :title %> <h4>あらすじ</h4> <%= f.text_area :summary %> <h4>見どころ</h4> <%= f.text_area :highlight %> <h4>ハッシュタグ入力欄</h4> <%= f.text_area :hashbody, size:"55x3" %> <h4><p><%= f.label :画像 %></p></h4> <p><%= f.file_field :image %></p> <div class="check_box"> <h4>ジャンル</h4> <%= f.collection_check_boxes(:tag_ids, Tag.all, :id, :name) do |tag| %> <div> <%= tag.label do %> <%= tag.check_box %> <%= tag.text %> <% end %> </div> <% end %> </div> </div> <%= f.submit '保存', class: "btn" %> <% end %> view/update </div> <h1>Posts#update</h1> <p>Find me in app/views/posts/update.html.erb</p>
試したこと
patch 'posts/:id' => 'posts#update'
をroutes.rbに加えてみましたが、ダメでした。
回答1件
あなたの回答
tips
プレビュー