Ruby On Rails - CRUD - Destroy/Delete Not Working?
Asked Answered
U

4

5

I've gone through a good chunk of this tutorial, and have gotten to the part where functionality for deleting articles is added:

https://guides.rubyonrails.org/getting_started.html

...but whenever the 'Delete' link is clicked on, the article is not deleted. Nothing happens, and a GET request is sent rather than a DELETE.

From the erb file:

<%= link_to 'Destroy', article_path(@article), data:{
  turbo_method: :delete,
  turbo_confirm: 'Are you sure?'
} %>

From the controller:

def destroy
    @article = Article.find(params[:id])
    @article.destroy
    redirect_to root_path, status: :see_other
end

After trying a solution that was mentioned in SO and in various Web pages (adding the below to my 'application.html.erb' file:

<%= javascript_include_tag 'application', "data-turbo-track": "reload" %>

...I get this error:

ActionView::Template::Error (The asset "application.js" is not present in the asset pipeline.

I did some digging and realized that the application scaffolder (rails new blog in this case) didn't create any Javascript directories much less add any JS files to them. I'm fine with adding them myself, but I don't even know what JS files Rails and/or Turbo is looking for. JQuery? Some other library?

Ideally, however, I'd like to know how to properly scaffold an app so that any necessary Javascript is included.

Thanks, Bryan

EDIT I was able to get the delete functionality working by using the 'non-turbo' method of deleting items and using 'button_to' instead of 'link_to' for the 'Delete' link. However-- I would still like to know why the app as shown in the tutorial doesn't work as expected, and how to use a link rather than a button to trigger the item deletion (not to mention why using turbo doesn't work).

Upset answered 10/2, 2022 at 5:9 Comment(3)
Did you build your assets? Usually bin/dev to start processes with foremanWhisk
There is no 'bin/dev' directory or executable in my project.Upset
Probably you had some error during application creation. So your app was not created completelyWhisk
U
0

So my answer is that there is no answer--- but I believe mechnicov is correct in saying that the app was not created correctly; I recreated the project in Linux, and everything works as expected. Apparently there is something wrong with my Ruby/Rails installation in Windows.

Upset answered 10/2, 2022 at 21:7 Comment(0)
G
6

I also encountered the same problem following the official documentation. And after doing hours of research, I managed to make the turbo functionality works just by adding this line in application.html.erb.

<%= javascript_include_tag "turbo", type: "module" %>

To be honest, I did not understand why this is working, and I am not 100% sure whether this is the correct way to use turbo in rails 7.

Glasser answered 13/6, 2022 at 1:14 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewCondonation
I
3

I was able to get this working!

Somewhere along the way of me trying things, I stumbled upon mention of the turbo-rails gem. I found a working solution with these pieces:

  1. Add this in the application.html.erb file:
    <%= javascript_include_tag "turbo", type: "module" %>
    
  2. Add gem "turbo-rails", "~> 1.0" to your Gemfile
  3. Run bundle install
  4. Run bin/rails turbo:install
    Note: not sure if this last step is necessary, but I was following instructions from here

Using these steps, the code from the rails guide works, along with the confirmation dialog. 🎉

Idiocrasy answered 5/3, 2023 at 6:4 Comment(0)
U
0

So my answer is that there is no answer--- but I believe mechnicov is correct in saying that the app was not created correctly; I recreated the project in Linux, and everything works as expected. Apparently there is something wrong with my Ruby/Rails installation in Windows.

Upset answered 10/2, 2022 at 21:7 Comment(0)
E
0

Found solution without needing to add the import tag manually:

  1. Turbo needs node or importmap-rails set up

    bin/rails importmap:install

  2. Now turbo can be installed

    bin/rails turbo:install

After this it started working in my application created following the "Getting started".

Exhibition answered 9/9 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.