現在CRUD処理を実行できるアプリケーションを作成し練習しています。(タスク管理)
CRUD作成でここまでなんとか進めることができたのですが、destroy削除機能が実装できず、エラーが表示されます。
$ rails routes
でルートの一覧を確認し、
DELETE /tasks/:id(.:format) tasks#destroy
これに沿った内容を調べて編集したつもりなのですが、抜け出せないでいます。編集したファイル内容は以下のとうりです。
一緒にみてもらえないでしょうか。お願いいたします。
:追記1
undefined method `id' for nil:NilClassとありますが、nilとは何もないよ。という意味と記憶しています。
idはここでは登録されたタスクだと認識していますが、前に新規登録されたものが保存されています。(今はクリエイトするとエラーが起きるが編集画面で確認できる)
:追記2
$ rails c
でparamsを実行すると
pry(main)> params NameError: undefined local variable or method `params' for main:Object from (pry):9:in `__pry__'
と出力される
##routes.rb
rb
1Rails.application.routes.draw do 2 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 3 resources :tasks 4 root to: 'tasks#index' 5end
##tasks_controller.rb
rb
1class TasksController < ApplicationController 2 def index 3 @tasks = Task.all 4 end 5 6 def new 7 @task = Task.new 8 end 9 10 def edit 11 @task = Task.find(params[:id]) 12 end 13 14 15 def create 16 @task = Task.new(tasks_param) 17 #@task = Task.new(title: params[:task][:title], content: params[:task][:content]) 18 19 if @task.save 20 redirect_to tasks_url 21 else 22 render :new 23 end 24 end 25 26 27 def update 28 @task = Task.find(params[:id]) 29 if @task.update(tasks_param) 30 redirect_to tasks_path 31 else 32 render :edit 33 end 34 end 35 36 37 def destroy 38 @task = Task.find(params[:id]) 39 @task.destroy! 40 redirect_to tasks_path 41 end 42 43 private 44 def tasks_param 45 params.require(:task).permit(:title, :content) 46 end 47end
##index.html.erb
rb
1<h1>タスク一覧</h1> 2<ul> 3 4 <% @tasks.each do |task| %> 5 <li> 6 title: <%= task.title %> 7 </li> 8 9 <li> 10 content: <%= task.content %> 11 </li> 12 13<!-- /tasks/:id(.:format) --> 14 <%= link_to '削除', task_path(@task.id), 15 method: :delete, data: { confirm: "削除してよろしいですか?", commit: "削除する", cancel: "やめる", title: "ご確認ください" } %> 16 <% end %> 17 18 19</ul>
tasks_controller.rb
rb
1class TasksController < ApplicationController 2 def index 3 @tasks = Task.all 4 end 5 6 def new 7 @task = Task.new 8 end 9 10 def edit 11 @task = Task.find(params[:id]) 12 end 13 14 15 def create 16 @task = Task.new(tasks_param) 17 #@task = Task.new(title: params[:task][:title], content: params[:task][:content]) 18 19 if @task.save 20 redirect_to tasks_url 21 else 22 render :new 23 end 24 end 25 26 27 def update 28 @task = Task.find(params[:id]) 29 if @task.update(tasks_param) 30 redirect_to tasks_path 31 else 32 render :edit 33 end 34 end 35 36 37 def destroy 38 @task = Task.find(params[:id]) 39 @task.destroy! 40 redirect_to tasks_path 41 end 42 43 private 44 def tasks_param 45 params.require(:task).permit(:title, :content) 46 end 47end
index.html.erb
rb
1<h1>タスク一覧</h1> 2<ul> 3 4 <% @tasks.each do |task| %> 5 <li> 6 title: <%= task.title %> 7 </li> 8 9 <li> 10 content: <%= task.content %> 11 </li> 12 13 14 <%= link_to '削除', task_path(task.id), 15 method: :delete, data: { confirm: "削除してよろしいですか?", commit: "削除する", cancel: "やめる", title: "ご確認ください" } %> 16 <% end %> 17 18 19</ul>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/11 11:56
2019/01/11 12:05
2019/01/11 15:29
2019/01/12 05:23
2019/01/12 06:16
2019/01/12 06:30