Add a value to a delete link in Rails? - ruby-on-rails

I currently have the following link_to in my Rails application:
<%= link_to 'Remove this Person', #person, :confirm => 'Are you sure?', :method => :delete, :class => 'important', :class => "minimal important" %>
I would like to add the person's name in place of the "this person" for the link.
I don't appear to be able to do:
<%= link_to 'Remove <%= #person.name %>', #person, :confirm => 'Are you sure?', :method => :delete, :class => 'important', :class => "minimal important" %>
I have seen this example online:
<%= link_to(#profile) do %>
<strong><%= #profile.name %></strong> -- <span>Check it out!</span>
<% end %>
# => <a href="/profiles/1">
<strong>David</strong> -- <span>Check it out!</span>
</a>
Any ideas on how to convert that into a delete link, with the confirmation box?

Try this.
<%= link_to "Remove #{#person.name}", #person, :confirm => 'Are you sure?', :method => :delete, :class => 'important', :class => "minimal important" %>

Related

Adding a class to link_to

I have been trying to add a class to my link_to for over an hour now. No matter what I try, I get an error.
<% if user_signed_in? %>
<li>
<%= link_to('Sign out', destroy_user_session_path, :method => :delete) %>
</li>
<% else %>
<li>
<%= link_to('Sign in', new_user_session_path) %>
</li>
<% end %>
I am trying to add :class => "page-scroll btn-signin" to both of my link_to lines.
I solved the problem
<%= link_to 'Sign out', destroy_user_session_path, :method => :delete, :class => "page-scroll btn-signin" %>
This code works correctly.
<%= link_to 'Sign in', new_user_session_path, :class => "page-scroll btn-signin" %>
and
<%= link_to 'Sign out', destroy_user_session_path, :method => :delete, :class => "page-scroll btn-signin" %>
More info
EDIT
Destructive actions should be performed as a form submission - link
use button_to on Sign Out.
<%= button_to "Sign out", destroy_user_session_path, :method=>:delete, :class => "page-scroll btn-signin" %>

Syntax error when passing method argument in link_to in rails

When I put :method => :delete in link_to I'm getting syntax error and I don't understand why.
Here's the code:
<%= link_to application_path(application), :method => :delete, :confirm => 'Are you sure?', :title => 'Delete task', :class => "color-red" do %>
<span class="glyphicon glyphicon-trash">
<% end %>

Ruby in Rails, destroy does not work in edit. routing to show

I have a problem with the destroy action.
It works in the index.html.erb very fine.
But I can't get it work in the edit.html.erb
It routes to the show even without asking for a permission.
Many solutions here say it has something to do with jQuery. Well as you can see, I tried all I could find.
<%= stylesheet_link_tag "application" %>
<!--Delete Problem in Edit-->
<%#= javascript_include_tag :all %>
<%#= javascript_include_tag :application %>
<%#= javascript_include_tag :default %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
Is there a solution that works for my situation?
This is my delete button in edit:
<%= content_tag(:a, :href => contact_path(#contact), :class => "btn btn-warning pull-right", :style => "margin:0; margin-right:15px;", confirm: 'Are you sure?', method: :delete) do %>
<%= t "list.button_delete" %>
<%#= link_to I18n.t(".list.delete"), contact, confirm: 'Are you sure?', method: :delete %>
<%#= link_to 'delete', contact_path(#contact), :method => :delete %>
<% end %>
I tried many things, as well.
The following is the action:
def destroy
##contact = Contact.find(params[:id])
#contact = current_user.contacts.find(params[:id])
#contact.destroy
redirect_to contacts_url,
alert: 'Successfully deleted the contact'
end
Replace
<%= content_tag(:a, :href => contact_path(#contact), :class => "btn btn-warning pull-right", :style => "margin:0; margin-right:15px;", confirm: 'Are you sure?', method: :delete) do %>
with
<%= content_tag(:a, :href => contact_path(#contact), :class => "btn btn-warning pull-right", :style => "margin:0; margin-right:15px;", data: {confirm: 'Are you sure?'}, data: {method: :delete}) do %>
It should be data: {confirm: 'Are you sure?'} instead of confirm: 'Are you sure?' and also data: {method: :delete} instead of method: :delete .
where, link_to method interpolates confirm: 'Are you sure?' as data-confirm="Are you sure?" and method: :delete as data-method="delete".
BUT content_tag method interpolates confirm: 'Are you sure?' as confirm="Are you sure?" and method: :delete as method="delete" so your javascript call is not getting invoked.
In your destroy action replace:
#contact = current_user.contacts.find(params[:id])
with:
#contact = #current_user.contacts.find(params[:id])

rails confirm before delete

Here is my rails link_to
<%= link_to 'Delete',url_for(action: :delete,id: #user.id),data: {confirm: "Are you sure?"} %>
I tried the above method but it is directly deleting without any alert message. What wrong I made. Can any one correct it.
Try this
<%= link_to 'Delete',url_for(action: :delete,id: #user.id),method: :delete, data: {confirm: "Are you sure?"} %>
Answer for rails 4.1.8 (question doesn't include version)
and standard resource (also not specified)
//= jquery_ujs must be included in application.js (possibly missing)
url is user_path(#user)
specify :method => :delete (missing)
confirm message within :data (was good)
destroy method in controller (not delete)
= link_to t(:delete) , user_path(#user), :method => :delete, :class => "btn btn-danger", :data => {:confirm => t(:are_you_sure )}
The url in the question seems to create a GET, rather than a DELETE method. This may work if :method was specified. imho it is unfortunate that there is no seperate named url helper for this.
Before Rails 7
<%= link_to 'Delete', url_for(action: :delete, id: #user.id),
method: :delete, data: {confirm: "Are you sure?"} %>
Rails 7 (with Turbo, out of the box)
<%= link_to 'Delete', url_for(action: :delete, id: #user.id),
data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>
link_to('Delete', {controller: :controller_name, id: id, action: :action_name}, confirm: "Are you sure you want to delete this?", method: :delete)
The answer by #installero didn't work for me in rails 7.0.3, I achieved a similar thing using button_to:
<%= button_to "Delete", #category, form: { data: { turbo_confirm: "Are you sure?" } }, method: :delete %>
I think you should use :method option
<%= link_to 'Delete',url_for(action: :delete,id: #user.id), method: :delete, confirm: "Are you sure?" %>
It's probably better to use button (and form) for this kind of action
Rails confirmation popup box for destroy action:
<%= link_to 'Show', article_path(article),{:class=>'btn btn-success show_article' }%>
<%= link_to 'Edit', edit_article_path(article),{:class=>'btn btn-warning edit'} %>
<%= link_to 'Destroy', '#', "data-toggle"=>"modal", "data-target" => "#delete-#{article.id}",:class=>'btn btn-danger' %>
<div class="modal fade" id="delete-<%= article.id %>" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">Confirmation Box</button>
</div>
<div class="modal-body">
<p>Are you sure to delete this article</p>
</div>
<div class="modal-footer">
<div class="modal-footer">
<%= link_to 'Delete', article_path(article), method: :delete, :class => 'btn btn-danger' %>
Cancel
</div>
</div>
</div>
</div>
</div>
</td>
since Rails 7 Hotwire Turbo was introduced
<%= link_to "Delete", #post, class: 'btn btn-danger',data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>
<%= button_to "Destroy this post", #post, method: :delete, class: 'btn btn-danger', form: { data: { turbo_confirm: "Are you sure?" }} %>
Check the following link:
<%= link_to 'Delete',url_for(action: :delete,id: #user.id), confirm: "Are you sure?" %>
Make sure you have both the jQuery library and the jQuery driver for Rails included. The jquery-rails gem will take care of both of these in the later versions of Rails, from what I know.
Also, for better chances of compatibility with future versions of the jQuery driver, move the :confirm option outside :data and let the jquery-rails gem decide what to do with it.

Adding a class to link_to block

I have a following code which displays a 'delete' link:
<%= link_to :class => 'some_class', :method => :delete, :data => { :confirm => 'Are you sure?' } do
<span>Delete</span>
<% end %>
But for some reason ROR is not adding some_class to a tag. Have you any idea what can i do to fix it ? Thanks in advance.
You need to add the URL as the first parameter, then the html options, e.g.:
<%= link_to resource_path(#resource), :class => 'some_class', :method => :delete, :data => { :confirm => 'Are you sure?' } do
<span>Delete</span>
<% end %>
I actually found this to be a working solution with Rails 4.2
<%= link_to(resource_path(#resource), class: "project-card clearfix") do %>
<h1>Your html here</h1>
<% end %>
If you need to pass a controller and action, like edit and destroy, do it as follow:
<%= link_to url_for(controller: controller_name, action: :edit, id: item.id), class: "btn btn-link btn-warning btn-just-icon edit" do %>
<i class="material-icons">edit</i>
<% end %>
<%= link_to url_for(controller: controller_name, action: :destroy, id: item.id), method: :delete, data: { confirm: t('common.confirm') }, class: 'btn btn-link btn-danger btn-just-icon remove' do %>
<i class="material-icons">close</i>
<% end %>
The link_to docs:
link_to(body, url, html_options = {})
So you'd want
<%= link_to <span>Delete</span>, '/someurl', :class=>'some_class', :method=>:delete, .... %>

Resources