Home > other >  Destroyed object still there?
Destroyed object still there?

Time:10-10

The "destroy" method is not working on my site. I created a "delete" link, but when I click it I just get redirected.

Here is the controller with the destroy method:

class PostsController < ApplicationController
  def index
    @posts = Post.all
  end

  def show
    @post = Post.find(params[:id])
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)

    if @post.save
      redirect_to @post
    else
      render :new, status: :unprocessable_entity
    end
  end

  def edit
    @post = Post.find(params[:id])
  end

  def update
    @post = Post.find(params[:id])

    if @post.update(post_params)
      redirect_to @post
    else
      render :edit, status: :unprocessable_entity
    end
  end

  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    redirect_to root_path, status: :see_other
  end

  private
    def post_params
      params.require(:post).permit(:title, :description, :image, :price)
    end
end

Nothing fancy. And here is the link in my posts/show.html.erb view:

<%= link_to 'Delete', root_path,
              method: :delete,
              data: { confirm: 'Are you sure?' } %>

Doesn't work - I just get redirected to the root path, and the post I tried to delete remains.

I tried removing a post in the rails console, and that did work.

Rails 7.0.4
Ruby 3.1.2

Server output after clicking "delete" link on local site:

Started GET "/posts/3" for ::1 at 2022-10-09 23:59:02 -0400
  ActiveRecord::SchemaMigration Pluck (0.2ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by PostsController#show as HTML
  Parameters: {"id"=>"3"}
  Post Load (0.1ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
  ↳ app/controllers/posts_controller.rb:7:in `show'
  Rendering layout layouts/application.html.erb
  Rendering posts/show.html.erb within layouts/application
  ActiveStorage::Attachment Load (0.1ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ?  [["record_id", 3], ["record_type", "Post"], ["name", "image"], ["LIMIT", 1]]
  ↳ app/views/posts/show.html.erb:11
  ActiveStorage::Blob Load (0.1ms)  SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/views/posts/show.html.erb:12
  Comment Load (0.1ms)  SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ?  [["post_id", 3]]
  ↳ app/views/posts/show.html.erb:23
  Rendered collection of templates [0 times] (Duration: 0.0ms | Allocations: 35)
  Rendered comments/_form.html.erb (Duration: 31.1ms | Allocations: 6334)
  Rendered posts/show.html.erb within layouts/application (Duration: 142.9ms | Allocations: 31474)
  Rendered layout layouts/application.html.erb (Duration: 300.1ms | Allocations: 53293)
Completed 200 OK in 402ms (Views: 312.5ms | ActiveRecord: 2.1ms | Allocations: 66385)


Started GET "/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSWhiR28xZVRKamJucHhZM0pxZFhaak9YZGxZelY0ZVdwbGREWnlNZ1k2QmtWVU9oQmthWE53YjNOcGRHbHZia2tpWldsdWJHbHVaVHNnWm1sc1pXNWhiV1U5SW1sc1h6RTFPRGg0VGk0ek1qSTNNRGc0TlRZMVh6SmlkWGd1Y0c1bklqc2dabWxzWlc1aGJXVXFQVlZVUmkwNEp5ZHBiRjh4TlRnNGVFNHVNekl5TnpBNE9EVTJOVjh5WW5WNExuQnVad1k3QmxRNkVXTnZiblJsYm5SZmRIbHdaVWtpRG1sdFlXZGxMM0J1WndZN0JsUTZFWE5sY25acFkyVmZibUZ0WlRvS2JHOWpZV3c9IiwiZXhwIjoiMjAyMi0xMC0xMFQwNDowMTo0My45NDVaIiwicHVyIjoiYmxvYl9rZXkifX0=--00b620d927983a0cba2300d10b1e2234b9306a84/il_1588xN.3227088565_2bux.png" for ::1 at 2022-10-09 23:59:03 -0400
Processing by ActiveStorage::DiskController#show as PNG
  Parameters: {"encoded_key"=>"[FILTERED]", "filename"=>"il_1588xN.3227088565_2bux"}
Completed 304 Not Modified in 7ms (ActiveRecord: 0.0ms | Allocations: 559)

CodePudding user response:

Looks like you have an issue with Turbo or Rails UJS.

These should really work and send a DELETE request via javascript:

# Turbo
link_to "delete", @post, data: { turbo_method: :delete }

# Rails UJS
link_to "delete", @post, method: :delete

If your javascript is broken than data and method attributes are ignored and you're just clicking a link to show @post, which is what's happening in the log.

On the other hand:

data-turbo-method changes the link request type from the default GET. Ideally, non-GET requests should be triggered with forms, but data-turbo-method might be useful where a form is not possible.

https://turbo.hotwired.dev/reference/attributes

To trigger a DELETE request with a form you can use button_to, which creates a little form and rails correctly handles routing to destroy action:

button_to "Delete", @post, method: :delete

https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

  • Related