前提
画像投稿のwebアプリをRuby on Rails6にて作成中です。
画像にはタグが付いており、削除時にエラーが出ます。
実現したいこと
投稿を削除したい
発生している問題・エラーメッセージ
NoMethodError in DesignsController#destroy undefined method `find' for PostForm:Class
該当のソースコード
ruby
1def destroy 2 post_form = PostForm.find(params[:id]) 3 if post_form.destroy 4 redirect_to root_path 5 end
ruby
1[app > controller.rb] 2 3class DesignsController < ApplicationController 4 before_action :authenticate_user!, only:[:edit, :new, :destroy] 5 before_action :move_to_index, except: [:index, :show] 6 before_action :set_design, only: [:show, :edit] 7 8 def index 9 @designs = Design.includes(:user).order("created_at DESC") 10 end 11 12 def new 13 @post_form = PostForm.new 14 end 15 16 def create 17 @post_form = PostForm.new(design_params) 18 19 if @post_form.save 20 redirect_to root_path 21 else 22 render :new 23 end 24 end 25 26 def show 27 28 end 29 30 def destroy 31 post_form = PostForm.find(params[:id]) 32 if post_form.destroy 33 redirect_to root_path 34 end 35 end 36 37 def update 38 design = Design.find(params[:id]) 39 if 40 design.update(design_params) 41 redirect_to design_path(design.id) 42 else 43 redirect_to request.referer 44 end 45 end 46 47 def edit 48 unless user_signed_in? && current_user.id == @design.user_id 49 redirect_to action: :index 50 end 51 end 52 53 def search 54 @designs = Design.search(params[:keyword]).order("created_at DESC") 55 end 56 57 private 58 59 def design_params 60 params.require(:post_form).permit(:title, :file_name, :tag_name, :image).merge(user_id: current_user.id) 61 end 62 63 def set_design 64 @design = Design.find(params[:id]) 65 end 66 67 def move_to_index 68 unless user_signed_in? 69 redirect_to action: :index 70 end 71 end 72 73end 74
ruby
1[app > views > designs > show.erb] 2 3<main class="main"> 4 <div class="inner"> 5 <div class="prototype__wrapper"> 6 <p class="prototype__hedding"> 7 <%= @design.title %> 8 </p> 9 <% if user_signed_in? && current_user.id == @design.user_id %> 10 <div class="prototype__detail"><br> 11 <p class="detail__title">ファイル場所</p> 12 <p class="detail__message"> 13 <%= @design.file_name %> 14 </p> 15 </div> 16 <div class="prototype__manage"> 17 <%# link_to "編集する", edit_design_path, method: :get, class: :prototype__btn %> 18 <%= link_to "削除する", design_path, method: :delete, class: :prototype__btn %> 19 </div> 20 <% end %> 21 22 <span class="tag_style">#<%=link_to @design.tags.first&.tag_name, search_designs_path(tag_id: tag.id)%></span> 23 24 <div class="prototype__image"> 25 <%= image_tag @design.image %> 26 </div> 27 28 </div> 29 </div> 30</main> 31
ruby
1[app > models > post_form.rb] 2 3class PostForm 4 include ActiveModel::Model 5 6 attr_accessor( 7 :title, :file_name, :image, 8 :id, :user_id, 9 :tag_name 10 ) 11 12 with_options presence: true do 13 validates :title 14 validates :file_name 15 validates :image 16 end 17 18 19 ActiveRecord::Base.transaction do 20 21 def save 22 design = Design.create!(title: title, file_name: file_name, image: image, user_id: user_id) 23 tag = Tag.where(tag_name: tag_name).first_or_initialize 24 tag.save! 25 PostTagRelation.create!(design_id: design.id, tag_id: tag.id) 26 end 27 28 29 30 return true 31 32 rescue false 33 end 34end 35
試したこと
def destroyをpost_formに書いたり、@をつけたり
def destroy
design = Design.find(params[:id])
if design.destroy
redirect_to root_path
end
end
に書き換えてみたり、
find(params[:id]) を PostForm.(design_params) に変えてみても
param is missing or the value is empty: post_form のエラーが出るだけでした。

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2023/01/18 08:44
2023/01/19 00:31
2023/01/19 00:33