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.

7
  • 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. Commented May 8, 2015 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. Commented May 8, 2015 at 17:49
  • 1
    show us your entry in config/routes.rb for this route please Commented May 8, 2015 at 17:52
  • It's sounding a lot like the application.js file is not included in the layout. Commented May 8, 2015 at 18:13
  • @ilanberci, updated my question with the list of routes Commented May 8, 2015 at 19:25

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I was having this same error and changing from "link_to" to "button_to" made my application work. Could you or somebody else please elaborate on why link_to does not work and whether there is a way to make it work as desired?
@EthanMelamed link_to creates the <a> tag for links. This tag is always a GET. JavaScript is then used to modify the link to become a DELETE request. This JavaScript helper is in rails.js. If either rails.js is not properly loaded, or JavaScript is not enabled in your browser, then it will stay a GET request. One workaround for to avoid the JavaScript is to use button_to, which provides a form, not a link.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.