前提・実現したいこと
Railsでアウトプットツールを作成しています。
マイページから投稿したものを削除しようとリンクに飛んだところ下記のエラーが出ました。(アラートで削除しますか?と表示されて進むと出る)
昨日から全然わからなくて詰まってしまったので、どなたかお願い致します。
発生している問題・エラーメッセージ
エラーメッセージ NoMethodError in PostsController#destroy undefined method `image_id' for #<Post:0x00007fe452285d50> Did you mean? image Extracted source (around line #32): 30 def destroy 31 post = Post.find(params[:id]) 32 post.destroy 33 redirect_to posts_path 34 end
該当のソースコード
app/controller/post_controller.rb class PostsController < ApplicationController before_action :set_post,only: [:show, :edit, :update] def index @posts = Post.includes(:user) end def show end def new @post = Post.new end def create @post = Post.new(post_params) @post.user_id = current_user.id @post.save redirect_to post_path(@post) end def edit end def update @post.update(post_params) redirect_to posts_path(@post) end def destroy post = Post.find(params[:id]) post.destroy redirect_to posts_path end private def post_params params.require(:post).permit(:title, :body, :impression) end def set_post @post = Post.find(params[:id]) end end
app/viwes/users/show.html.erb ---省略 </section> <div class="contents row"> <p class="title is-4"><%= @user.username %>さんの投稿一覧</p> <section class="section"> <div class="container"> <div class="columns is-multiline"> <% @posts.each do |post| %> <div class="column is-7"> <div class="card"> <div class="card-content"> <div class="media"> <div class="media-content"> <p class="title is-5"><%= post.title %></p> </div> </div> <div class="content"> <table class="table is-narrow"> <span class="name"><%= simple_format post.body %></span> </table> </div> <div class="card-content"> <div class="media"> <div class="media-content"> <%= link_to post_path(post), class: "panel-block" do %> <span class="panel-icon"> <i class="fas fa-book" aria-hidden="true"></i> </span> このアウトプットを見る <% end %> <% if @user.id == current_user.id %> <%= link_to edit_post_path(post), class: "panel-block" do %> <span class="panel-icon"> <i class="fas fa-edit"></i> </span> このアウトプットを編集する <% end %> <%= link_to post_path(post), method: :delete, data: {confirm: "削除しますか?"}, class: "panel-block" do %> <span class="panel-icon"> <i class="fas fa-trash"></i> </span> このアウトプットを削除する <% end %> <% end %> </div> </div> <div class="content"> <time><%= post.updated_at.strftime("%Y-%m-%d %H:%M") %></time>更新 </div> </div> </div> </div> </div> </div> <% end %> </div> <div> </section> </div>
Gemfile source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.6.5' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 6.0.0' # Use mysql as the database for Active Record gem 'mysql2', '>= 0.5.3' # Use Puma as the app server gem 'puma', '~> 3.11' # Use SCSS for stylesheets gem 'sass-rails', '~> 5' # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker gem 'webpacker', '~> 4.0' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.7' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use Active Model has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Active Storage variant # gem 'image_processing', '~> 1.2' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.2', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 2.15' gem 'selenium-webdriver' # Easy installation and use of web drivers to run system tests with browsers gem 'webdrivers' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] gem 'pry-rails' gem 'devise' gem "refile", require: "refile/rails", github: 'manfe/refile' gem "refile-mini_magick" gem "bulma-rails"
試したこと
application.jsに追記 // This file is automatically compiled by Webpack, along with any other files // present in this directory. You're encouraged to place your actual application logic in // a relevant structure within app/javascript and only use these pack files to reference // that code so it'll be compiled. require("@rails/ujs").start() require("turbolinks").start() require("@rails/activestorage").start() require("channels") // Uncomment to copy all static images under ../images to the output folder and reference // them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>) // or the `imagePath` JavaScript helper below. // // const images = require.context('../images', true) // const imagePath = (name) => images(name, true) #ここから追記 //= require activestorage //= require turbolinks //= require jquery //= require popper //= require bootstrap //= require_tree . //= require jquery_ujs //
app/viwes/layouts/application.html.erb <!DOCTYPE html> <html> <head> <title>Hondeput</title> <%= csrf_meta_tags %> <%= csp_meta_tag %> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.min.css" integrity="sha256-UzFD2WYH2U1dQpKDjjZK72VtPeWP50NoJjd26rnAdUI=" crossorigin="anonymous" /> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> </head> 【変更内容】 <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> → <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>に変更。 ※webpackerも確認しましたが、Rails6から導入されているらしくそこはいじっておりません。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。