It's possible to have a working link_to without jQuery
I've found the best process to make a working delete link for ruby on rails without jQuery! Here I already make an answer: https://mcmap.net/q/543571/-ruby-on-rails-link_to-delete-method-not-working
But for making this easy, I'm writing this here again.
We need to work with 3 things:
- Adding destroy method in articles_controller.rb
- routes.rb setup
- LINK_TO tag
Let's Start...
Adding destroy method in articles_controller.rb
At first, we will add def destroy ... end
in articles_controller.rb
,
lets open:
# app/controllers/articles_controller.rb
def destroy
@article = Article.find(params[:id])
@article.destroy
params[:id] = nil
flash[:notice] = "Art has been deleted"
redirect_to :action => :index
end
Here
- in the 1st line, we're calling a variable '@article' which will find and select the ID parameters of the article on rails console from our database. Then,
- in 2nd line, the @article variable will make the destroy command in the console. then,
- in 3rd line: the id params will be deleted and
- in 4th line, a notice will flash in application page "Art has been deleted" and also will show in console that, found nothing in the database.
- In 5th line, after the destroying process completed, we will be redirected to the article index page.
This is the main factor which will give the destroying command. and make the link_to working.
Setup routes.rb
BUT WAIT
We need 2 routes for the destroy page which are:
- A GET protocol setup
- A DELETE protocol setup
In routes just add:
resources :articles, except: [:destroy] # this will add all get request links automatically except destroy link
post '/articles/new' => 'articles#create'
post '/articles/:id' => 'articles#update'
post '/articles/:id/edit' => 'articles#update' # this 3 lines are needed for other forms purpose
# we need this 2 lines for our delete link_to setup
delete 'articles/:id/delete' => 'articles#destroy', as: 'articles_delete'
get '/articles/:id/delete' => 'articles#destroy'
Here
The 2nd last line is declaring the DELETE method,
- 'articles/:id/delete' will be the link structure in post link tag (known as: anchor tag in HTML) for every single post,
- '=>' is pointing the link structure to the controller tag which is 'articles#destroy',
- then we defined the path text by setting ** as: 'articles_delete'** which we will use as:
'articles_delete_path' or 'articles_delete_url' in link_to tag.
Then
- in last line, we defined the get request for the delete link which will give us a working link like "https://2haas.com/articles/1/delete" except "/articles/1/destroy" that means we can customize our delete link from this 2 methods setup with more additional information..
Last sweetest delete output
The desired link_to tag
we can use this to get proper delete link_to tag which will work.
<%= link_to 'Delete Article', articles_delete_path, method: :delete %>
<%= link_to 'Delete Article', articles_delete_url, method: :delete %>
<% obj.each do |post| %>
<%= link_to 'Delete Article', articles_delete_path(post), method: :delete %>
<% end %>
AND that's done except jQuery
Thanks for reading this answer properly.!
HAPPY PROGRAMMINGS