実現したいこと
ここに実現したいことを箇条書きで書いてください。
- ▲▲機能を動作するようにする
updateメソッドで更新ができないを解決したい。
前提
railsえtodoアプリの開発練習しています。
更新機能を実装中にエラーが発生しました。
Started PATCH "/tasks/:id" for ::1 at 2023-06-10 21:29:23 +0900 Processing by TasksController#update as TURBO_STREAM Parameters: {"authenticity_token"=>"[FILTERED]", "task"=>{"title"=>"rfew"}, "commit"=>"更新", "id"=>":id"} Task Load (0.2ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]] ↳ app/controllers/tasks_controller.rb:25:in `update' Completed 404 Not Found in 16ms (ActiveRecord: 0.2ms | Allocations: 1229) ActiveRecord::RecordNotFound (Couldn't find Task with 'id'=:id):
該当のソースコード
ruby
1class TasksController < ApplicationController 2 3 def index 4 @tasks = Task.all 5 end 6 7 def new 8 9 end 10 11 def create 12 Task.create(title:params[:title]) 13 redirect_to "/tasks" 14 end 15 16 def edit 17 @task = Task.find(params[:id]) 18 end 19 20 def update 21 @task = Task.find(params[:id]) 22 @task.update(title: params[:title]) 23 redirect_to "/tasks" 24 end 25 26 def destroy 27 @task = Task.find(params[:id]) 28 @task.destroy 29 redirect_to "/tasks" 30 end 31end
edit.html.erb
1<h2>EDIT TASK</h2> 2<%= form_with model: @task, url: "/tasks/:id", local: true do |f| %> 3 <p><%= f.text_field :title %></p> 4 <p><%= f.submit "更新" %></p> 5<% end %>
routes.rb
1Rails.application.routes.draw do 2 get "tasks" => "tasks#index" 3 get "tasks/new" => "tasks#new" 4 post "tasks/create" => "tasks#create" 5 get "tasks/:id/edit" => "tasks#edit" 6 patch "tasks/:id" => "tasks#update" 7 delete "tasks/:id" => "tasks#destroy" 8end 9
試したこと
ルーティングのupdateへ渡すところを以下のように変更しましたが、更新できませんでした。
routes.rb
1 変更前: 2 patch "tasks/:id" => "tasks#update" 3 4変更後: 5 patch "tasks/:id" => "tasks#update", as: :update
それに伴いform_withのurlを以下のように変更
edit.html.erb
1変更前: 2<%= form_with model: @task, url: "/tasks/:id", local: true do |f| %> 3 4変更後: 5<%= form_with model: @task, url: update_path, local: true do |f| %> 6
ストロングパラメーターを設定してやっても見ましたが同じようなエラーです。
以下エラーのTURBO_STREAMが原因かを調べていましたが、正確に理解できませんでしたが、おそらくJavaScriptが動いているように理解しています。
しかし、form_withでlocal: trueにしているので問題ないと考えています。
Processing by TasksController#update as TURBO_STREAM
今の状態はupdateメソッドに値を渡しきれていないと考えています。
なぜなのか教えていただけますでしょうか。
ヒントや可能性でも思いついたら教えていただけますでしょうか。
よろしくお願いします。
補足情報(FW/ツールのバージョンなど)
ruby 3.2.2
Rails 7.0.5
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/06/11 01:17