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)
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:
index.html.erb:
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
: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:
Controller looks like this:
When I navigate to an article and hit the destroy link I get the following server logs:
So, what I think I’m seeing here is:
What’s the emoji for ‘in over my head’? 🤯