No route matches [POST] - Rails destroy - ruby-on-rails

I am new to RoR and still don't have enough experience on solving the different errors that may appear to me. In this case I am designing a blog where I can post articles. More specifically, my problem is related to deleting these articles.
As far as I know, writing:
resources :articles
in the routes file is an alternative for writing:
get "/articles" #index
post "/articles" #create
delete "/articles/:id" #delete
get "/articles/:id" #show
get "/articles/new" #new
get "/articles/:id/edit" #edit
patch "/articles/:id" #update
put "/articles/:id" #update
When I try to delete an article I get the following error:
No route matches [POST] "/articles/1"
The code I wrote was:
View
<% #articles.each do |art| %>
<%= art.title %>
<div>
<%= art.body %> - <%= link_to "Delete", art, method: :delete %>
</div>
<% end %>
Controller
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end

It sounds like you have this in your view:
<%= art.body %> - <%= link_to "Delete", art, method: :destroy %>
But you actually need:
<%= art.body %> - <%= link_to "Delete", art, method: :delete %>
I'd advise double-checking this in your app based on your reply to a comment from #GonzaloRobaina.

It looks to me like you're missing the correct path in your code. It should work with something like this :)
<%= link_to "Delete, article_path(art), method: :delete %>

Related

No route matches [DELETE] "/tags"

Edited to include config/routes.rb file.
I'm working through the Jumpstartlab Blogger 2 tutorial and I've run into trouble trying to delete tags from a tag list. This is my first Rails project and I'm still trying to wrap my head around MVC and routing.
Here's the code from my Tags view:
<h1>All Tags</h1>
<ul id="tags">
<% #tags.each do |tag| %>
<li>
<%= link_to tag.name, tag_path(tag), class: 'tag_name' %>
<%= link_to "Delete", tags_path(#tag), method: :delete, data: {confirm: "Really delete the tag?"} %>
</li>
<% end %>
</ul>
And the code from my Tags controller:
class TagsController < ApplicationController
def show
#tag = Tag.find(params[:id])
end
def index
#tags = Tag.all
end
def destroy
#tag = Tag.find(params[:id])
#tag.destroy
end
end
And config.routes.rb:
Blogger::Application.routes.draw do
root to: 'articles#index'
resources :articles do
resources :comments
end
resources :tags
end
The error I'm getting is No route matches [DELETE] "/tags".
I feel like the issue is something basic that I haven't quite grasped yet. I would really appreciate if someone could help me understand what I've missed and how it works. If I haven't provided enough information, please let me know. And thanks!
It's because the DELETE route is based on an instance (just 1 tag) of a resource, not the collection (the group of tags).
So you have to change this line:
<%= link_to "Delete", tags_path(#tag), method: :delete, data: {confirm: "Really delete the tag?"} %>
To use tag_path(tag):
<%= link_to "Delete", tag_path(tag), method: :delete, data: {confirm: "Really delete the tag?"} %>

Rails 4: No route matches [PATCH] "/"

Route:
display_panel GET /display/panel(.:format) display#panel
Controller:
class DisplayController < ApplicationController
def panel
#approval = Approval.where(user_id: current_user.id)
end
end
View:
<%= form_for approval,url: root_path, method: :patch do |f| %>
<%= f.check_box :pass%>
<%= f.submit %>
<% end %>
I think I do patch in a get action, this causes the error, so I hope to use mathod: :patch to correct it, but it still show me the same error.
I tried many solutions from other similar questions, still not work.
Update your form like this:
<%= form_for #approval, url: [update_path] , method: :patch do |f| %>
<%= f.check_box :pass%>
<%= f.submit %>
<% end %>
[update_path] should be something like approval_path, not root_path. Run rake routes | grep approval to verify if you have setup patch route for approval.

link to last post - rails

Im trying to link_to the show action for a post.
In my controller I have:
#post = Post.all
In my view I'm trying to link it to the last post like this:
<%= link_to #post.last do %>
<%= #post.last.title %>
<% end %>
But it does not take me to the show page for that post?
Post.all loads all posts, but does not guarantee an order. You might want to use the id or the created_at value to order your list of posts:
# in your controller
#posts = Post.order(:id)
# in the view:
<%= link_to #posts.last.title, #posts.last %>
Or - if you don't need the other posts in the view - just load the lastest post:
# in the controller:
#latest_post = Post.order(:id).last
# in the view:
<%= link_to #latest_post.title, #latest_post %>
Try with below code,
<%= link_to post_path(#post.last) do %>
<%= #post.last.title %>
<% end %>
If this code not work then please find route with fire rake routes in your terminal and replace post_path with your routes
Hope this will work.

Deleting with link_to from within a Rails 4 view

The following Rails 4 link_to is wrong, and thus I'm unable to delete and not sure why. In this project, "bookmarks" is a nested resource under "users" so rake routes gives me:
DELETE /users/:user_id/bookmarks/:id(.:format) bookmarks#destroy
View:
<% #bookmarks.each do |bookmark| %>
<%= link_to "delete", user_bookmarks_path(#user, bookmark.id), method: :delete %>
<% end %>
Controller:
def destroy
#user.bookmarks.find(params[:id]).destroy
redirect_to root_path
end
private
def bookmark_params
params.require(:bookmark).permit(:title, :bookmark_url)
end
def get_user
#user = User.friendly.find(params[:user_id])
end
The result is a link that looks like http://www.example.com/users/jane-doe/bookmarks.6 where 6 is the correct ID of the bookmark to be deleted. But I don't understand why it's not creating /bookmarks/6, which I think would then work fine with destroy in my controller. It feels like there's some big conceptual piece I'm just not understanding. Any tips are appreciated.
Your view should look something like this. It appears your path name is incorrect:
<% #bookmarks.each do |bookmark| %>
<%= link_to "delete", user_bookmark_path(#user, bookmark.id), method: :delete %>
<% end %>

undefined method `edit_comment_path' (Rails)?

I have a Post model and a Comment model (which is a nested resource of the first model):
resources :posts do
resources :comments
end
posts/show.html.erb:
<%= render #comments %>
I think I have some error in here:
comments/_comment.erb
<%= link_to "Edit Post Comment", [#post, edit_comment_path(comment)] %>
because I get this error:
undefined method `edit_comment_path' for #<#<Class:0xb439d8c>:0xaeaf4c0>
Any suggestions to fix this?
If you run rake routes you can see the route names, in your case the route name should be edit_post_comment_path rather than just edit_comment_path.
Maybe <%= link_to "Edit Post Comment", [#post, :edit, comment] %> or <%= link_to "Edit Post Comment", edit_post_comment_path(#post, comment) %> (untested, can't test here).
Because edit_comment_path is, like rails says, undefined.

Resources