I'm trying to setup a link that deletes a database entry, and everything works as intended, it just doesn't ask for confirmation first. I think I've done everything right, I'm testing this in firefox on ubuntu.
<%= link_to "Delete", #post, :confirm => "Are you sure you want to delete?", :method => :delete %>
any help is appreciated.
Read the documentation carefully:
:data - This option can be used to add custom data attributes.
Data attributes
confirm: 'question?' - This will allow the unobtrusive JavaScript driver to prompt with the question specified (in this case, the
resulting text would be question?. If the user accepts, the link is
processed normally, otherwise no action is taken.
Your link_to should be like this:
<%= link_to "Delete", #post, data: { :confirm => "Question?", :method => :delete } %>
Look at this if you use a version before 4.0.2
Do you have this lines in your application.js file ?
//= require jquery
//= require jquery_ujs
Related
Can someone help me why the following two pieces of code do not yield the same 'result'?
Option 1
<%= button_to 'delete', {:controller => "articles", :action => 'destroy', :id => article.id},
:confirm => "Are you sure you want to delete?", :method => :delete %>
Option 2
<%= link_to 'delete', {:controller => "articles", :action => 'destroy', :id => article.id},
:confirm => "Are you sure you want to delete?", :method => :delete %>
Option 1 works. Option 2 for some reason calls the Show action in the controller. Funny thing is that Option 2 is used in the Ruby On Rails tutorial, which I was following....
As you can imagine I am a novice to ROR.
In order for link_to to work with the delete method, Rails needs the unobtrusive scripting adapter for jQuery.
Make sure that your Gemfile has
gem 'jquery-rails'
Make sure that app/assets/javascripts/application.js has
//= require jquery
//= require jquery_ujs
Make sure that your app/views/layouts/application.html.erb has
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
inside the head tag. Remove the 'data-turbolinks-track' => true section if you don't plan to use Turbolinks.
Since browsers can't send natively extended set of HTTP verbs (DELETE, PUT, PATCH). Rails uses the Rack::MethodOverride middleware to fake it.
So when you do a POST request with the _method = DELETE param the request object that your Rails app receives has request.method == 'DELETE'.
Recommended reading:
Rails on Rack
How do forms with PATCH, PUT, or DELETE methods work?
button_to creates a discrete form. So your "button" is really a form with hidden inputs.
<form action="articles/1" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit">This is the button you see</button>
</form>
Also Rails is pretty damn awesome so you can simply do:
<%= button_to 'delete', article,
confirm: "Are you sure you want to delete?", method: :delete %>
And it will figure out the route by itself as long as you follow the rails conventions.
link_to uses javascript to enhance a normal <a> element. Instead of the normal behavior the jQuery ujs driver creates a form like the above and submits it.
The most common reason that you get problems with link_to is that you don't have jquery and jquery_ujs included in your application.js or that you have a script error which is preventing it from doing its job. Check the browser console for errors.
With Rails I love how you can just do,
link_to "Click", some_path, :data => {:confirm => "Are you sure?"}.
Is there a simple way to pop up an alert dialog instead? Could I try something like,
link_to "Click", some_path, :data => {:ALERT => "Are you sure?"}?
Rails only supports :confirm out of the box, because it's convenient for delete links. All it really does is add "data-confirm" attribute to html tag, then javascript logic is applied to any tag that has this attribute. You could make your 2nd version work by adding your own javascript code, similar to how confirm works.
You can see the code that makes :confirm possible here: https://github.com/rails/jquery-ujs/blob/master/src/rails.js
I just started coding in ruby on rails and I've been following a guide which is using a more outdated version of rails than I am using. I am using 3.2.12
This is my code:
<%= button_to 'Destroy', product, :method => "delete", :confirm => 'Are you sure?' %>
From what I understand, these are symbols that are passed to rails, which is then converted to either an html or javascript action that then pops up the message box and deletes the object if applicable. The above code destroys the object, but it does not pop up the confirm box. Why is this? Also, I had the above as the following at first:
<%= link_to 'Destroy', product, :method => "delete", :confirm => 'Are you sure?' %>
The confirm box is not popping up under any circumstance, using link_to or button_to. Below is the html rendered when inspected using Chrome's inspector. jquery and jquery-ujs are loaded into the as well, so I'm not sure where to go from here.
<input name="_method" type="hidden" value="delete">
<input data-confirm="Are you sureeee?" type="submit" value="Destroy">
<input name="authenticity_token" type="hidden" value="Q2xicqELHYHtrwarbtPBe5PT2bZgWV5C+JdcReJI8ig=">
Thanks!
I had to add my confirm attribute inside the data attribute to get it to work. I am using rails 4 with bootstrap. I hope this helps someone else who has that issue.
link_to 'Delete', #rule, method: :delete, data: { confirm: 'Are you sure you want to delete this alert?' }
This relies on jQuery, ensure you have the following:
in your Gemfile
group :assets do
gem 'jquery-rails'
end
in your assets/javascripts/application.js file, before the line //= require_tree .
//= require jquery
//= require jquery_ujs
The difference between link_to and button_to is the HTTP verb. link_to issues GET requests and button_to issues POST requests. With the RESTful routing, the delete action is a POST request to controller/id. If you issue a GET to controller/id, it is dispatched to the show action.
AIUI, link_to with anything other than the GET verb is a Bad Idea. One, right-clicks don't preserve the verb. Two, you don't want bots crawling the page to follow the link and thereby trigger the delete action even though you probably need to be logged in to actually modify the database.
Feel pretty dumb, but adblock was blocking the message box. Sorry about that. All is well now, I just disabled adblock.
If you want to delete something with confirmation box in rails 7, you may try this one:
With button_to (more prefered IMHO):
<%= button_to 'Destroy', product, method: :delete,
form: {data: {turbo_confirm: 'Are you sure?'}} %>
This will render an HTML form tag which sends a POST request on submit (after confirmation) but with a hidden _method attribute with 'delete' value. So that rails will treat this request as if it has a DELETE method.
It will be routed to products#destroy (or whatever your routes are saying).
With link_to:
<%= link_to 'Destroy', product,
data: {turbo_method: :delete, turbo_confirm: 'Sure?'} %>
This will render a simple a tag with data-turbo-method and data-turbo-confirm attributes. Clicking this link will trigger a confirmation box and if "OK" is chosed, a real DELETE request will be sent.
If you want to end destroy action in your controller with a redirect_to, some browsers will redirect to a new location with DELETE method (causing errors), so make sure to add status: :see_other parameter, like the guides suggests.
I have a pop-up blocker running in Chrome. I just whitelisted http://localhost:3000 and it worked for me.
In a form, I have:
= link_to "Delete toy", #toy, :method => :delete, :confirm => "Are you sure?", :class => "btn btn-danger"
This generates the following html:
Delete toy
However, when I click the link, I simply get taken back to the show page of the toy. (I don't get the confirm popup either.) What am I doing wrong?
I added a = javascript_include_tag :all and = javascript_include_tag "application" inside my application.html.haml, per the recommendation a bunch of the other posts on SO about this exact question, but it didn't help.
UPDATE: this is what is in my application.js file:
//= require jquery
//= require jquery_ujs
//= require_tree .
Remove the = javascript_include_tag :all part of your layout. This is probably loading jQuery-UJS twice, the second overwriting all configured listeners.
With the asset pipeline, you'll only ever need to include "application", and never all the other files.
(same happens with the stylesheets)
I have the following link_to delete url in my app
<%=link_to "Delete",blog_path(#blog.id), :method => :delete, :class => "delete", :confirm => "Are you sure ?"%>
It does not seem to be working.When I click this url, it just takes me to the show path.Can someone please tell me how to fix this. Thanks.
Are you using jQuery? If so, I think the problem could be that you are using jQuery without the updated rails.js file.
Download rails.js here:
https://github.com/rails/jquery-ujs/raw/master/src/rails.js
Drop it in your javascripts directory, overwriting the rails.js that comes default with rails.
Add a javascript include line to include it.
<%= javascript_include_tag "rails" %>
Put this after your Jquery include tag. You probably also want to disinclude the javascript defaults if you don't plan on using prototype.
I included jQuery UI in my application, I found that delete is now working as show, but after doing above Resolved Issue.
Make sure these lines appear in application.js :
//= require jquery
//= require jquery_ujs
Ensure that you have java script turned on. Otherwise :method => :delete acts just as show in Rails.
If you're using restful routing for blogs, then the following should work:
<%= link_to "Delete", #blog, :method => :delete, :confirm => "Are you sure ?"%>
You can try with 'data-method' instead of :method.
<%=link_to "Delete",blog_path(#blog.id), 'data-method' => :delete, :class => "delete", :confirm => "Are you sure ?"%>
You can check on jquery_ujs.js the following piece of code:
// Handles "data-method" on links such as:
// Delete
In order for link_to to work with the delete method, Rails needs the unobtrusive scripting adapter for jQuery.
Make sure that your Gemfile has
gem 'jquery-rails'
Make sure that app/assets/javascripts/application.js has
//= require jquery
//= require jquery_ujs
Make sure that your app/views/layouts/application.html.erb has
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
inside the head tag. Remove the 'data-turbolinks-track' => true section if you don't plan to use Turbolinks.
you should use
<%=button_to "Delete",blog_path(#blog.id), :method => :delete, :class => "delete", :confirm => "Are you sure ?"%>
It's possible to have a working link_to without jQuery
I've found the best process to make a working delete link for ruby on rails without jQuery! Here I already make an answer: https://stackoverflow.com/a/67710994/14387700
But for making this easy, I'm writing this here again.
We need to work with 3 things:
Adding destroy method in articles_controller.rb
routes.rb setup
LINK_TO tag
Let's Start...
Adding destroy method in articles_controller.rb
At first, we will add def destroy ... end in articles_controller.rb,
lets open:
# app/controllers/articles_controller.rb
def destroy
#article = Article.find(params[:id])
#article.destroy
params[:id] = nil
flash[:notice] = "Art has been deleted"
redirect_to :action => :index
end
Here
in the 1st line, we're calling a variable '#article' which will find and select the ID parameters of the article on rails console from our database. Then,
in 2nd line, the #article variable will make the destroy command in the console. then,
in 3rd line: the id params will be deleted and
in 4th line, a notice will flash in application page "Art has been deleted" and also will show in console that, found nothing in the database.
In 5th line, after the destroying process completed, we will be redirected to the article index page.
This is the main factor which will give the destroying command. and make the link_to working.
Setup routes.rb
BUT WAIT
We need 2 routes for the destroy page which are:
A GET protocol setup
A DELETE protocol setup
In routes just add:
resources :articles, except: [:destroy] # this will add all get request links automatically except destroy link
post '/articles/new' => 'articles#create'
post '/articles/:id' => 'articles#update'
post '/articles/:id/edit' => 'articles#update' # this 3 lines are needed for other forms purpose
# we need this 2 lines for our delete link_to setup
delete 'articles/:id/delete' => 'articles#destroy', as: 'articles_delete'
get '/articles/:id/delete' => 'articles#destroy'
Here
The 2nd last line is declaring the DELETE method,
'articles/:id/delete' will be the link structure in post link tag (known as: anchor tag in HTML) for every single post,
'=>' is pointing the link structure to the controller tag which is 'articles#destroy',
then we defined the path text by setting ** as: 'articles_delete'** which we will use as:
'articles_delete_path' or 'articles_delete_url' in link_to tag.
Then
in last line, we defined the get request for the delete link which will give us a working link like "https://2haas.com/articles/1/delete" except "/articles/1/destroy" that means we can customize our delete link from this 2 methods setup with more additional information..
Last sweetest delete output
The desired link_to tag
we can use this to get proper delete link_to tag which will work.
<%= link_to 'Delete Article', articles_delete_path, method: :delete %>
<%= link_to 'Delete Article', articles_delete_url, method: :delete %>
<% obj.each do |post| %>
<%= link_to 'Delete Article', articles_delete_path(post), method: :delete %>
<% end %>
AND that's done except jQuery
Thanks for reading this answer properly.!
HAPPY PROGRAMMINGS