Home>

I'm creating an output tool in Rails.
When I jumped to the link to delete what I posted from My Page, I got the following error. (Are you sure I want to delete it with an alert?

I didn't understand at all from yesterday and I got stuck, so please someone.

Error message
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
Corresponding source code
app/controller/post_controller.rb
class PostsController</pre>
<pre><code>app/viwes/users/show.html.erb
---abridgement</section><p><% = @ user.username%>'s post list</p><section>            <% @ Posts.each do | post |%>                                                                                       <p><% = post.title%></p>                                              <table>                <span><% = simple_format post.body%></span>              </table>                                                                                         <% = link_to post_path (post), class: "panel-block" do%>                      <span>                        <i aria-hidden = "true"></i>                      </span>                        See this output
                      <% end%>                    <% if @ user.id == current_user.id%>                      <% = link_to edit_post_path (post), class: "panel-block" do%>                        <span>                          <i></i>                        </span>                            Edit this output
                        <% end%>                      <% = link_to post_path (post), method:: delete, data: {confirm: "Are you sure I want to delete?"},

 class: "panel-block" do%>                        <span>                          <i></i>                        </span>                          Delete this output
                        <% end%>                    <% end%>                                                                               <time><% = post.updated_at.strftime ("% Y-% m-% d% H:% M")%></time>Update
                                                                                                <% end%>        </section>
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.rbgem'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"
What I tried
Added to 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)
#Addition from here
// = require active storage
// = 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>[Changes]
  <% = javascript_include_tag'application','data-turbolinks-track':'reload'%>→<% = javascript_pack_tag'application','data-turbolinks-track':'reload'%>.
* I also checked the webpacker, but it seems that it was introduced from Rails 6, and I have not tampered with it.
Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    posts.rb

    class Post</pre>
    <p><br />
    ↓ Change</p>
    <pre><code>attachment: image # delete


    An error occurred because it was described in posts.rb even though image was not used at the time of posting.

  • Answer # 2

    The Destroy action does not send the ID from the front, so I think the following is good.

    def destroy
        @ post.destroy
        redirect_to posts_path
      end