0

I have controller destroy method

def destroy
    @category = Category.find(params[:id])
    @category.destroy
    redirect_to backend_categories_path
end

and in view two different destroying links

<%= link_to 'Destroy', backend_category_path(category), method: :delete, data: { confirm: 'Are you sure?' } %>
<%= button_to 'Destroy', backend_category_path(category), :confirm => "are you sure?", :method => :delete %>

problems are:

  1. 1st link doesnt work, it shows record not delete
  2. 2nd link does work but dont ask me if i really want to delete record.

jquery gem is installed whats problem ?

rails version is 3.2.13

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
Muflix
  • 6,192
  • 17
  • 77
  • 153
  • Does the first link still hit the controller method? E.g. if you put a log message in the controller like `puts 'logging here'` will it still show up? – Ryan Endacott Jun 13 '13 at 21:56
  • From this question, it also looks like you should always use a `button_to` for a delete: http://stackoverflow.com/questions/4606860/rails-3-link-to-to-destroy-not-working – Ryan Endacott Jun 13 '13 at 21:59
  • Be careful with links to destroys; they might be crawled. – Dave Newton Jun 13 '13 at 22:01
  • For the second link issue, here is a similar problem, maybe this solution could help: http://www.ruby-forum.com/topic/471271 – Ryan Endacott Jun 13 '13 at 22:02
  • Thank you very much, solution is i had bad `javascript_include_tag` (yes, sorry :D) in html layout that thing with crawler is interesting, but it is on site needed authorization so it shouldnt be dangerous other way i think i can post via ajax. (im new so i cant answer this topic now but i will) – Muflix Jun 13 '13 at 23:33

2 Answers2

0

Indeed, you should not have any destructive actions accessible via GET request. Among other concerns, you run the risk of the things being deleted when your site is crawled.

That said, your use of button_to is syntactically correct. The failure of the confirmation message to work is likely attributable to a Javascript error. Have you ensured that you have the following in your <head>?

# app/views/home/index.html.erb
<head>
    <%= javascript_include_tag(:defaults) %>
    <%= csrf_meta_tag %>
</head>
zeantsoi
  • 25,857
  • 7
  • 69
  • 61
0

in the head of your application layout have you added <%= javascript_include_tag "application" %> ? like this :

<!DOCTYPE html>
<html>
<head>
<title>title of page</title>
.....
.....
<%= javascript_include_tag "application" %> 

<%= csrf_meta_tag %>

</head>
....

and be sure that you have the following lines in your app/assets/application.js file :

//= require jquery
//= require jquery_ujs 
//= require_tree .

i think that you can also replace <%= button_to Destroy',backend_category_path(category), .... with <%= button_to 'Destroy', category, ....

medBouzid
  • 7,484
  • 10
  • 56
  • 86