I want a link to show depending on whether or not a user is an admin (which is a boolean attribute). So far I have this:
views/opportunities_opportunity <%= link_to_if(#user.admin?, "Delete", opportunity, method: :delete, data: {confirm: 'Are you sure?'}) %>
This will make the link active if and only if the user is an admin. Therefore, the "Delete" appears, but it is not a link to anywhere. I am afraid that it might confuse non-admins into thinking the link is broken. Does anyone know if there something along the lines of link_visible_if(#user.admin?)?
Please help! Thanks!
You could just use an if statement.
<%= link_to("Delete", opportunity, method: :delete, data: {confirm: 'Are you sure?'}) if #user.admin? %>
Related
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
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?
I have attempted to use the following code to add a confirmation message to a logout option in a dropdown menu, but it's not working.
<%= link_to "Logout", destroy_user_session_path, method: :delete, data: {confirm: 'Are you sure?'}, class: "dropdown-item" %>
I feel like I either made a random misspelling or I'm missing something glaringly obvious. Thanks in advance!
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?
I'm diving into Ruby on Rails and I find that when I'm using the scaffold code that RoR generated for my model, it causes a confirmation box to appear that reads "Are you sure?" when I click on the delete link. What causes this to appear and how do I remove it? Thanks, I'm just trying to figure out how this all works.
The scaffold link_to will look something like this:
<%= link_to "Destroy", #code, :confirm => 'Are you sure?', :method => :delete %>
Removing the :confirm => 'Are you sure?', argument to the link helper will remove the javascript confirmation alert.
The link in the template has a :confirm=>"My message here" option. If you remove that option, then it won't have a message.