user3181365 user3181365 - 1 month ago 6
Ruby Question

Rails delete functionality is not working

I am completely new to Ruby On Rails, and going through this guide to build a basic application.

When I am trying to implement the delete functionality as mentioned in the document, I am seeing the show page.

I have below method in my controller:

def destroy
@article = Article.find(params[:id])
@article.destroy

redirect_to articles_path
end


and below line in my page:

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


Now when I click on the link, I am seeing below URL in browser:


http://localhost:3000/articles/1


Now now in this case I am seeing the show screen instead of getting an alert message and then deleting the record from my page.

I have followed this SO post - Rails 4 link_to Destroy not working in Getting Started tutorial

and verified that

//= require jquery
//= require jquery_ujs


elements are there in my
application.js


Please tell me where I am doing mistake?

Update:

Here is my output of
rake routes
command:

E:\Rails\blog\bin>rake routes
(in E:/Rails/blog)
Prefix Verb URI Pattern Controller#Action
welcome_index GET /welcome/index(.:format) welcome#index
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
root GET / welcome#index


This is what I get from log files when I click on Delete link:

Started GET "/articles/3" for 127.0.0.1 at 2015-05-09 00:56:08 +0530
Processing by ArticlesController#show as HTML
Parameters: {"id"=>"3"}
[1m[35mArticle Load (0.0ms)[0m SELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT 1 [["id", 3]]
Rendered articles/show.html.erb within layouts/application (0.0ms)
Completed 200 OK in 47ms (Views: 46.9ms | ActiveRecord: 0.0ms)


In my
application.html.erb
file, I have modified the text
application
to
default
to fix an issue that I reported in this post - ExecJS::ProgramError in Welcome#index TypeError: Object doesn't support this property or method , is this the cause of my issue now? If revert the change, then the application is failing completely.

Answer

You have to use button_to instead of link_to to make it work.

Refer to this post - Rails 3 - link_to :delete is redirecting to show action with jquery installed , look at the answer given by Cypher.

Comments