annie_apnasf
annie_apnasf

Reputation: 35

Rails tutorial: delete method doesn't work

I'm following instructions of this tutorial: http://guides.rubyonrails.org/getting_started.html I have a problem with deleting articles. I've written the code as the tutorial suggests, and link 'Destroy' has appeared, but when I click it, instead of asking me for a confirmation and deleting the article, it shows me the article's page, i.e. it does exactly the same thing as when I click on 'Show'. This is the place in my code that fails to perform what I want it to:

<td><%= article.title %></td>
        <td><%= article.text %></td>
        <td><%= link_to 'Show', article_path(article) %></td>
        <td><%= link_to 'Edit', edit_article_path(article) %></td>
        <td><%= link_to 'Destroy', article_path(article),
          method: :delete,
          data: { confirm: 'Are you sure?' } %></td>

I'd appreciate any help!

The destroy method inside the ArticlesController:

def destroy
    @article = Article.find(params[:id])
    @article.destroy
    redirect_to articles_path, alert: "Article successfully deleted"
end

routes.rb:

Rails.application.routes.draw do
    get 'welcome/index'
    resources :articles
    root 'welcome#index'
end

This is the log after I click on the Destroy link (I'm sorry it's so long, I don't know what's most important):

Started GET "/articles/1" for ::1 at 2016-05-23 14:22:16 +0300  
Processing by ArticlesController#show as HTML  
Parameters: {"id"=>"1"}  
Article Load (0.0ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT 1  [["id", 1]]

Rendered articles/show.html.erb within layouts/application (1.0ms)  
Completed 200 OK in 136ms (Views: 92.3ms | ActiveRecord: 0.0ms)  

Started GET "/stylesheets/default.css" for ::1 at 2016-05-23 14:22:16 +0300  
ActionController::RoutingError (No route matches [GET] "/stylesheets/default.css"):

actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'  
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'  
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'  
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'  
actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'  
railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'  
railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'  
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'  
activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'  
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'  
railties (4.2.6) lib/rails/rack/logger.rb:20:in `call'  
actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'  
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'  
rack (1.6.4) lib/rack/runtime.rb:18:in `call'  
activesupport (4.2.6)     lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'  
rack (1.6.4) lib/rack/lock.rb:17:in `call'  
actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call'  
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'  
railties (4.2.6) lib/rails/engine.rb:518:in `call'  
railties (4.2.6) lib/rails/application.rb:165:in `call'  
rack (1.6.4) lib/rack/lock.rb:17:in `call'  
rack (1.6.4) lib/rack/content_length.rb:15:in `call'  
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'  
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'  
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'  
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'  

Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.0ms)  
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (3.0ms)  
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/routes/_table.html.erb (2.0ms)  
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms)  
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (2763.6ms)  
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (1.0ms)  
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (1.0ms)  
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.0ms)  
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.0ms)  
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (1896.9ms)  
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (0.0ms)  
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (1.0ms)  
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (4376.6ms)  


Started GET "/javascripts/default.js" for ::1 at 2016-05-23 14:22:25 +0300

ActionController::RoutingError (No route matches [GET] "/javascripts/default.js"):
  actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.6) lib/rails/engine.rb:518:in `call'
railties (4.2.6) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'


Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/routes/_route.html.erb (3.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/routes/_table.html.erb (2.5ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.5ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (2777.2ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (1.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (1.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string (1.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript (1912.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript (1.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (1.0ms)
Rendered C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb (4408.9ms)

Upvotes: 1

Views: 299

Answers (2)

Fred
Fred

Reputation: 1627

Delete links relies on Rails ujs. Do you have javascript enabled on your browser ?

Upvotes: -1

Yimanei
Yimanei

Reputation: 242

In your controller you should have a destroy method like this:

def destroy
    @article = Article.find(params[:id])
    @article.destroy
    redirect_to articles_path, alert: "Article successfully deleted"
end

Upvotes: -1

Related Questions