1

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.

Community
  • 1
  • 1
learner
  • 6,062
  • 14
  • 79
  • 139
  • Have a look in `log/development.log` to see what route is being trigger and what errors, if any, are generated. When working on a Rails app you should probably have a `tail -f log/development.log` or equivalent open all the time to catch mistakes like this early. – tadman May 08 '15 at 17:44
  • 1
    @tadman He probably started the Rails app with `rails server`. That will boot the app and show the development log, but yes... you shouldn't see `Started GET "/articles/1" ...` in your log, since you are using `method: :delete`. – Kevin May 08 '15 at 17:49
  • 1
    show us your entry in config/routes.rb for this route please – ilan berci May 08 '15 at 17:52
  • It's sounding a lot like the `application.js` file is not included in the layout. – tadman May 08 '15 at 18:13
  • @ilanberci, updated my question with the list of routes – learner May 08 '15 at 19:25
  • @tadman, added output from log file, also I have updated with what changes I made to application.html.erb, please check. – learner May 08 '15 at 19:27
  • @newmediafreak, please check the log output – learner May 08 '15 at 19:28

2 Answers2

9

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.

Community
  • 1
  • 1
Chaitanya
  • 15,403
  • 35
  • 96
  • 137
0

This post is already solved but for those who ran into the same problem using rails guides 7.0.3 where turbo was used this method can be used to solve it

gem install turbo, rails importmap:install, rails turbo:install, rails turbo:install:redis,

in the exact order. note: there is no need to change link_to to button_to