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
-
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
Related articles
- ruby on rails - error when vagrant up
- ruby on rails - nomethoderror in itemscontroller # new error
- ruby on rails - i want to resolve rails name error
- ruby on rails - about passing view and controller data with rails
- ruby on rails 6 - i want to solve the error that occurred while implementing the product listing function
- ruby - an error occurs when building a rails 6 environment with docker
- ruby on rails - i want to download a file with rails, s3 and carrierwave, but i get an error
- ruby on rails - unknown action error when trying to post a comment
- ruby on rails - i don't understand the content of the error
- ruby on rails - error when implementing rails6 follow function
- ruby on rails - syntax error cannot be resolved
- ruby on rails 6 - [error] uninitialized constant user (call'userconnection' to establish a connection) :: image
- ruby on rails 6 - when i press new user registration, i get an error of no route matches [get] "/ users"
- ruby on rails - i get an error without being created
- ruby - how do you see the rails controller relative path? (carrierwave directory deleted)
- ruby on rails - rails routing error
- error in building environment of ruby on rails
- ruby on rails 6 - error resolution for no route matches [delete]
- ruby on rails - rspec validation error
- ruby - how to pass values from rails controller to view
Related questions
- ruby on rails - when i run rails, i get an error related to webpacker
- ruby - [rails6] i get an error on heroku when i write
- ruby - i want to be able to purchase by pressing the purchase button
- ruby on rails - webpacker cannot be compiled
- ruby - i want to display the edit button and delete button on the index page
- ruby - i want to display new posts i made on the top page only for myself
- ruby on rails - cannot start unicorn cannot load `railsconfigactive_storageservice`
- ruby - dealing with validation errors when rendering rails apps (definition of instance variables)
- ruby - about rails controller new action
- ruby on rails - no response when trying to launch ec2 rails
posts.rb
An error occurred because it was described in posts.rb even though image was not used at the time of posting.