rails: Deleting article in section 6.5 of getting started guide doesn't work

Steps to reproduce

Total newcomer to Ruby and Rails, enjoying the tutorial 😄. I’ve followed the Edge Getting Started guide step by step, but when I get to step 6.5 I’ve hit this issue.

I’ve pushed my repo here

Expected behavior

When clicking the ‘Destroy’ link on the article page, the article should be removed from the database.

Actual behavior

The article is requested from the server and displayed again

When I click the destroy link I notice in the logs that the server receives a GET request for the article: Started GET "/articles/1" for ::1 at 2021-10-11 14:09:56 +0100

I think what’s happening is that some UJS should be looking for this link based on the data-method="delete" attribute, and changing the GET to a DELETE (or maybe a POST that does a DELETE). I’m guessing that the JS isn’t running, so a simple GET is issued.

I’ve spotted some historic discussion that the assets are not being compiled so the jquery is missing, but that looks out of date. I’ve also tried running the server in development mode using bin/rails server -e development, but see the same problem.

System configuration

Running on MacOS (11.6) on MacBook Pro M1, tested in Safari & Chrome Rails version: 7.0.0.alpha2 Ruby version: 3.0.0p0 (2020-12-25 revision 95aff21468) [arm64-darwin20]

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 13
  • Comments: 17 (10 by maintainers)

Most upvoted comments

Pull request #43430 raised @ghiculescu

If anyone would like to open a PR to correct these usages, please do!

See this discussion about the redirect issue. This was never addressed as far as I can see.

About the tutorial: maybe button_to should be used for the delete link, as using <a> for destructive actions is discouraged. That would work regardless of JS framework.

Some progress (I think)

By telling the controller to respond to the DELETE request with a redirect_to with status 303, I get the behaviour I think we’re looking for.

Not sure this is the simplest or canonical way to achieve the result though…

in articles_controller.rb:

def destroy
    @article = Article.find(params[:id])
    @article.destroy

    redirect_to root_path, status: 303
  end

index.html.erb:

<ul>
  <li><%= link_to "Edit", edit_article_path(@article) %></li>
  <li><%= link_to "Destroy", article_path(@article),
                  "data-turbo-method": :delete,
                  data: { confirm: "Are you sure?" } %></li>
</ul>

NOTE, I still don’t get a confirmation dialog for the deletion. Another puzzle…

Was excited to see the release of Rails 7 and have been going through the getting started page and found myself stuck due to this issue. I expect others may bounce off in frustration when the getting started guide doesn’t work vs. digging for the workaround. Looking forward to seeing the PR merged shortly!

FYI: a workaround to show a confirmation dialog via Turbo on link_to:

<%= link_to 'Destroy', pattern_path(pattern), "data-turbo-method": "delete", "data-turbo-confirm": 'Are you sure?' %>

This works in 7.0alpha2 by now, and I feel this is not official.

Think I’ve stumbled into another issue…

My show.html.erb looks like this:

<h1><%= @article.title %></h1>

<p><%= @article.body %></p>

<ul>
  <li><%= link_to "Edit", edit_article_path(@article) %></li>
  <li><%= link_to "Destroy", article_path(@article), "data-turbo-method": :"delete" %></a></li>
  </ul>

Controller looks like this:

def destroy
    @article = Article.find(params[:id])
    @article.destroy

    redirect_to root_path
  end

When I navigate to an article and hit the destroy link I get the following server logs:

Started GET "/articles/8" for ::1 at 2021-10-11 21:57:00 +0100
Processing by ArticlesController#show as HTML
  Parameters: {"id"=>"8"}
  Article Load (0.1ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT ?  [["id", 8], ["LIMIT", 1]]
  ↳ app/controllers/articles_controller.rb:7:in `show'
  Rendering layout layouts/application.html.erb
  Rendering articles/show.html.erb within layouts/application
  Rendered articles/show.html.erb within layouts/application (Duration: 0.2ms | Allocations: 71)
  Rendered layout layouts/application.html.erb (Duration: 4.7ms | Allocations: 2346)
Completed 200 OK in 7ms (Views: 5.2ms | ActiveRecord: 0.1ms | Allocations: 3097)


Started DELETE "/articles/8" for ::1 at 2021-10-11 21:57:09 +0100
Processing by ArticlesController#destroy as TURBO_STREAM
  Parameters: {"id"=>"8"}
  Article Load (0.3ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT ?  [["id", 8], ["LIMIT", 1]]
  ↳ app/controllers/articles_controller.rb:39:in `destroy'
  TRANSACTION (0.1ms)  begin transaction
  ↳ app/controllers/articles_controller.rb:40:in `destroy'
  Article Destroy (0.5ms)  DELETE FROM "articles" WHERE "articles"."id" = ?  [["id", 8]]
  ↳ app/controllers/articles_controller.rb:40:in `destroy'
  TRANSACTION (1.3ms)  commit transaction
  ↳ app/controllers/articles_controller.rb:40:in `destroy'
Redirected to http://localhost:3000/
Completed 302 Found in 7ms (ActiveRecord: 2.2ms | Allocations: 2079)


Started DELETE "/" for ::1 at 2021-10-11 21:57:09 +0100
  
ActionController::RoutingError (No route matches [DELETE] "/"):
  
Started GET "/articles/8" for ::1 at 2021-10-11 21:57:09 +0100
Processing by ArticlesController#show as HTML
  Parameters: {"id"=>"8"}
  Article Load (0.1ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT ?  [["id", 8], ["LIMIT", 1]]
  ↳ app/controllers/articles_controller.rb:7:in `show'
Completed 404 Not Found in 1ms (ActiveRecord: 0.1ms | Allocations: 870)


  
ActiveRecord::RecordNotFound (Couldn't find Article with 'id'=8):
  
app/controllers/articles_controller.rb:7:in `show'

So, what I think I’m seeing here is:

  1. A delete sent to the article path
  2. A redirect being served to the root path
  3. The browser retaining the delete verb and requesting a delete on the root path.
  4. a routing error since that’s an unsupported route.
  5. a crash when the app issues a GET for the article which has now been deleted.

What’s the emoji for ‘in over my head’? 🤯