最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ruby On Rails - CRUD - DestroyDelete Not Working? - Stack Overflow

programmeradmin2浏览0评论

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

.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).

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/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).

Share Improve this question edited Feb 10, 2022 at 6:13 Bryan Green asked Feb 10, 2022 at 5:09 Bryan GreenBryan Green 5215 silver badges18 bronze badges 3
  • Did you build your assets? Usually bin/dev to start processes with foreman – mechnicov Commented Feb 10, 2022 at 9:00
  • There is no 'bin/dev' directory or executable in my project. – Bryan Green Commented Feb 10, 2022 at 14:51
  • Probably you had some error during application creation. So your app was not created pletely – mechnicov Commented Feb 10, 2022 at 14:59
Add a ment  | 

4 Answers 4

Reset to default 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.

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.

发布评论

评论列表(0)

  1. 暂无评论