2

I am getting started with Ruby and implementing simple blog from their official materials.

I finish the app, but have an issue with destroying action. Here is the form

<h1>Listing Articles</h1>
 
<%= link_to 'New article', new_article_path %>

<% if !@articles.nil? %>
  
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th colspan="3"></th>
  </tr>
 
  <% @articles.each do |article| %>
    <tr>
      <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>
    </tr>
  <% end %>
</table>

<% else %>
  
  <p> no avaiable articles </p>

<% end %>

And that is available routes

          Prefix Verb   URI Pattern                                  Controller#Action
   welcome_index GET    /welcome/index(.:format)                     welcome#index
            root GET    /                                            welcome#index
article_comments GET    /articles/:article_id/comments(.:format)     comments#index
                 POST   /articles/:article_id/comments(.:format)     comments#create
 article_comment GET    /articles/:article_id/comments/:id(.:format) comments#show
                 PATCH  /articles/:article_id/comments/:id(.:format) comments#update
                 PUT    /articles/:article_id/comments/:id(.:format) comments#update
                 DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
        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

You may noticed delete method on form link and DELETE request is mapped on articles#destroy action. However on the click on this link a GET call instead of DELETE is invoked (the same URI pattern) and I have not found yet the root cause.

The routing by itself is below

Rails.application.routes.draw do
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  
  get 'welcome/index'
  
  root 'welcome#index'

  post '/articles/:id', to: 'articles#update'

  resources :articles, only: [:new, :edit, :index, :create, :show, :update, :destroy] do 
    resources :comments
  end

end

I successed with defining alias for explicitly exposed route, but besides introducing new issues it is not uses platform native way resources :articles

Could you help with understanding\finding the cause of this issue?

Joe Dow
  • 323
  • 2
  • 7

1 Answers1

0

The problem is that link_to helper tag need UJS to handle delete, make sure you add it to application.js file

//= require jquery
//= require jquery_ujs

if in rails 6 you need

require("@rails/ujs").start()
Ninh Le
  • 1,291
  • 7
  • 13
  • Thanks for your input. It didn't help. If you would like to dive deeper in the issue I put my code into the repo https://github.com/Gelassen/ruby-samples-and-interviews. (p.s. my app doesn't have assets/javascripts/application.js, it doesn't have assets at all) – Joe Dow Aug 08 '20 at 06:34
  • yeah, I see, you have initialized app with API mode, so it has no assets directory, that why your delete method of link_to helper doesn't work corrrectly – Ninh Le Aug 08 '20 at 06:55
  • is it possible in Ruby to convert project to a different type at this stage? – Joe Dow Aug 08 '20 at 07:06
  • yes, you just need to run `rails new ruby-samples-and-interviews `, you can read about detail [here](https://stackoverflow.com/a/41991039/8476609) – Ninh Le Aug 08 '20 at 07:13