Rationale for changing syntax to make DELETE call in Rails 7 - ruby-on-rails

So Rails 7 changed the way that JavaScript is handled, replacing the UJS functionality with turbo components.
As part of this change, the syntax to create a link that emits a DELETE request has changed.
Rails 6 example:
<%= link_to 'Delete', author_path(author), method: :delete, data: { confirm: 'Are you sure?' } %>
Rails 7 example:
<%= link_to 'Delete', author_path(author), data: {turbo_method: :delete, turbo_confirm: "Are you sure?" } %>
This means that during an update to Rails 7 all these types of calls using the link_to helper need to be identified and adapted.
DHH has written a blog post on the changes, but I don't see the rationale that necessitates the changed syntax in the link_to helper here.
Am I missing something?

Related

Rails 7 - Devise do not delete user account

My View:
<%= link_to 'delete account', user_registration_path, :method => :delete, :class=>
'delete_account_button', data: {:confirm =>'sure?', disable_with: "deleting..."} %>
and I'm redirected to localhost:3000/users ... nothing more. No console errors, logs just show that I was redirected ...
FIRST WORKING EXAMPLE:
Changed link_to to button_to and it work. Why?
From the rails guides, you need a data turbo method to make it work
<%= link_to "Destroy", article_path(#article), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %>
So update from data key with something similar
data: { 'turbo-method': :delete, 'turbo-confirm': 'Are you sure?' }
turbo documentation
button_to
Is used to submit a form and send either a POST or DELETE request
link_to
Is used redirect_to a page through GET requests
Turbo is not necessary here to make the button work. However if you wanted to reload elements on the dom you would need turbo.
Check this tutorial out if you want to learn turbo and Hotwire for Rails 7.
https://www.hotrails.dev/turbo-rails
It helped me out a lot

submit_tag is not displaying confirmation box

In my .html.haml file I added the following submit_tag:
= submit_tag 'Set Master', class: 'btn btn-primary', id: 'btn-set-master', data: { confirm: "Are you sure?" }
But no confirmation box is showing up when I click on the submit button.
This post suggests there was a bug in Rails.
Not sure if that bug still exists or if I am doing something wrong.
What version of Rails do you use? Rails 3 didn't require data param, just confirm: 'Are you sure?'

How do you pass multiple arguments to nested route paths in Rails?

I am new to Rails and normally set up a link_to helper for a normal unnested route like so:
link_to "Delete", article_path(article.id), method: :delete, data: {confirm: "Are you sure?"}
However I am learning about nested routes and it seems I need to provide the path with two arguments for example:
link_to "(Delete)", article_comments_path(comment.article_id, comment.id), method: :delete, data:{comfirm: 'Are you sure?'}
However this does not seem to work. I have seen that you can format the link_to like this:
link_to 'Destroy Comment', [comment.article, comment], method: :delete, data: { confirm: 'Are you sure?' }
But this seems confusing to me as there is no path defined and the values necessary for the path arn't directly specified either.
Is it possible to format a nested link_to path like the unnested one above or does it need to be formatted as shown in the third example? If so could someone try to explain how this array format is translated into a url for Rails to execute?
Thanks
Route:
article_comment_path - DELETE - /articles/:article_id/comments/:id(.:format) - comments#destroy
I think your route would be something like articles/:article_id/comments/:id so you can do:
<%= link_to "Delete", article_comments_path(article_id: comment.article_id, id: comment.id), method: :delete, data:{comfirm: 'Are you sure?'} %>
And your 3rd link should be
<%= link_to 'Destroy Comment', polymorphic_path(comment.article, comment), method: :delete, data: { confirm: 'Are you sure?' } %>
For details check Polymorphic routes
Update
You just need to pass locals to your partial like:
<%= render "comment", locals: {article: comment.article, comment: comment} %>
and then use
<%= link_to "Delete", article_comments_path(article.id,comment.id), method: :delete, data:{comfirm: 'Are you sure?'}%>

Using :confirm in my link_to helper method in ruby on rails 4 has no effect what so ever. What could be wrong?

This is my link_to method:
<%= link_to 'Dispatch Now', order_path(order["objectId"]), :method => 'delete', :confirm => 'Are you sure?' %>
I've looked at my old ruby on rails 3 projects and this is how I've called confirm in my link_to helpers.
It doesn't seem to have any effect.
Has something changed with ruby on rails 4 that would have caused this to stop working? I have the jquery-rails file in my gemfile and I've checked my application.js file and everything looks fine.
What could be wrong?
You'll need to use:
<%= link_to "Delete", path, method: :delete, data: { confirm: "Are you sure?"} %>
--
Rails 4 changed the syntax, so that the confirm attribute is now handled in the data hash. Whereas before you could get away with confirm:, now you have to manage data: { confirm: "your_confirmation" }

Rails delete links not working after upgrade

With rails 2.3.8 I had
= link_to 'old_Delete', link_path(#link), :confirm => 'Are you sure?',
:method => :delete
With rails 3.1.8 this no longer works, no error and the log just shows a show request.
I tried changing it to
= link_to 'Delete', link_path(#link), method: :delete,
data: { :confirm => 'Are you sure?' }
but that didn't help either.
How can I have a delete link that works.
I created a new app (rails 3.2.7) with a 'post' resource to see how links look in a new app.
The link generated for that was:
= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' }
but when I try to use that format, replacing post (Post resource, model, etc.) with #link, the link instance that I am using elsewhere on the page, i.e.
= link_to 'Destroy', #link, method: :delete, data: { confirm: 'Are you sure?' }
It doesn't work (no error).
I tried saving rails.js in my public/javascripts folder (I have assest compilation turned off) and including config.action_view.javascript_expansions[:defaults] = %w(jquery rails) in my config/application.rb but it didn't help, still seeing gets and no deletes happening.
As others have said you probably have a typo, link vs #link. But the reason the delete stopped working is probably centered around the change to UJS. Long answer, google "rails change to ujs in 3.0". Short answer, make sure your app is loading rails.js from an appropriate ujs helper provider, such as https://github.com/rails/jquery-ujs
Make sure that, along with your other changes you change:
= javascript_include_tag "application"
to
= javascript_include_tag :defaults
In the last approach is your variable "link" or "#link"? Could you try using the later one?

Resources