0

I am working on rails 7. I'm trying to run my 'Delete' method after disabling 'Turbo' but its not working, it keeps getting to my 'show.html.erb' file instead of deleting the specific data. Here is my 'Delete' action code:

<td>

    <%= link_to 'Delete', friend_path(friend),  data: { turbo:false ,confirm: 'Are you sure?'}, method: :delete, :class=>"btn btn-danger"%>

  </td>

And below is the controller method to destroy/delete data:

def destroy 
debugger
@friend = Friend.find(params[:id])
@friend.destroy

if @friend.destroy
    redirect_to friends_path, notice: "Deleted Successfully!!!"
else
  render :index, status: :unprocessable_entity

end

end

MHZ GAMER
  • 11
  • 2
  • Did you decide to switch from turbo to rails-ujs? Did you use it in your project? Some JS magic has to happen. Why did you disable turbo? – mechnicov Jun 28 '22 at 10:38
  • i'm new on rails bro, so i don't know too much about turbo. I wanted to use AJAX on my delete method. My senior told me that first you need to process the delete request through HTML, not with turbo. So thats why i disable turbo... – MHZ GAMER Jun 29 '22 at 07:20

1 Answers1

0

Delete is not working is because you are not loading jQuery after disabling 'Turbo'.

Add jquery to your layout or page

<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

and it should start working again

MZaragoza
  • 10,108
  • 9
  • 71
  • 116
  • I have already included jquery using 'jquery-rails' gem and i also included this jquery in 'application.html.erb' file, which you just provided but still its not working... – MHZ GAMER Jun 28 '22 at 06:13
  • 2
    JQuery? How is this related? jquery-ujs is not used by rails >= 5 – mechnicov Jun 28 '22 at 10:34
  • yes it is...because after using jquery-ujs my problem is solved here is the link to solve this problem: https://stackoverflow.com/questions/72100292/rails-7-destroy-not-working-and-only-running-show – MHZ GAMER Jun 29 '22 at 07:22