前提・実現したいこと
railsで簡易的なTodoリスト見たなものを作成しています。
view画面でデータを追加するところまでできました。
それで追加はできたので削除機能を実装していたら実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
undefined method `destroy' for nil:NilClass //下記でエラー @course.destroy
該当のソースコード
html
1<table> 2 <thead> 3 <tr> 4 <th>A</th> 5 <th>B</th> 6 <th>time</th> 7 </tr> 8 </thead> 9 10 <tbody> 11 <% @courses.each do |course| %> 12 <tr> 13 <td><%= course.content %></td> 14 <td> 15 <%= form_tag("/destroy") do %> 16 <input type="submit" value="delete"> 17 <% end %> 18 </td> 19 <td><%= course.created_at %></td> 20 </tr> 21 <% end %> 22 </tbody> 23</table> 24<%= form_tag("/create") do %> 25<input type="text" name="content"> 26<input type="submit" value="投稿"> 27<% end %>
ruby
1Rails.application.routes.draw do 2 get '/' => 'booking#top' 3 get '/sub' => 'booking#sub' 4 post '/create' => 'booking#create' 5 post '/destroy' => 'booking#destroy'
ruby
1class BookingController < ApplicationController 2 def top 3 @courses = Post.all.order(created_at: :asc) 4 @times = Item.all 5 @course = Post.find_by(id: params[:id]) 6 end 7 8 def create 9 @course = Post.new(content: params[:content]) 10 @course.save 11 redirect_to("/") 12 end 13 14 def destroy 15 @course = Post.find_by(id: params[:id]) 16 @course.destroy 17 18 redirect_to("/") 19 end 20end
試したこと
<%= form_tag("/destroy") do %> ```をcourse.contentまで含めて括ってみたのですが、できませんでした。 そもも、クラスが空でエラーが起きていると言っているので上記の試した子で出来るわけがないのですが、どうすればいいのか分からないので教えて頂きたいです。 ### 補足情報(FW/ツールのバージョンなど) rails5.2
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/29 23:26