Rails error while trying to delete: "No route matches [GET]"

540 views
Skip to first unread message

Arthur Flores Duarte

unread,
Jun 15, 2015, 2:25:54 AM6/15/15
to rubyonra...@googlegroups.com
Hello,

I am a newbie with Rails and I am having a problem with my study application (Rails 3.2.6), when using Destroy links, I get the following message:
Routing Error
No route matches [GET] "/comments/2"

I can see that the Destroy link are calling a GET command instead of a delete method.

I've searched a lot about this problem, but couln't find the reason, hope anyone can help me. 


_comment.html.erb:

<h4><%= comment.name %> said:</h4>
<%= simple_format comment.body %>
Sent <%= time_ago_in_words comment.created_at %> ago
<%= link_to "Destroy", comment_path(comment), method: :delete , :data => { :confirm => "Are you sure?"} %>


comments_controller.rb:

class CommentsController < ApplicationController
 def create
@job = Job.find(params[:job_id])
@comment = @job.comments.build(params[:comment])
if @comment.save
 flash[:notice] = "Comment was created with success!"
else
 flash[:alert] = "Please fill in all fields to create a comment."
end
redirect_to @job
 end

 def destroy
@comment = Comment.find(params[:id])
@comment.destroy
redirect_to @comment.job, notice: "Comment destroyed with success."
 end
end

rake routes:
           root        /                                jobs#premium
companies_login GET    /companies/login(.:format)       login#new
               POST   /companies/login(.:format)       login#create
     companies POST   /companies(.:format)             companies#create
   new_company GET    /companies/new(.:format)         companies#new
  job_comments POST   /jobs/:job_id/comments(.:format) comments#create
          jobs GET    /jobs(.:format)                  jobs#index
               POST   /jobs(.:format)                  jobs#create
       new_job GET    /jobs/new(.:format)              jobs#new
      edit_job GET    /jobs/:id/edit(.:format)         jobs#edit
           job GET    /jobs/:id(.:format)              jobs#show
               PUT    /jobs/:id(.:format)              jobs#update
               DELETE /jobs/:id(.:format)              jobs#destroy
       comment DELETE /comments/:id(.:format)          comments#destroy
   hello_world GET    /hello/world(.:format)           hello#world

Rafael Belo

unread,
Jun 15, 2015, 11:51:47 PM6/15/15
to rubyonra...@googlegroups.com
Post your route.rb plz.

tamouse pontiki

unread,
Jun 16, 2015, 6:38:10 AM6/16/15
to rubyonra...@googlegroups.com
Arthur,

It hasn't been all that long since I've used 3.2, but I *think* you're using the wrong syntax. Please look at http://apidock.com/rails/v3.2.8/ActionView/Helpers/UrlHelper/link_to that shows the link_to method at that time. 

Also, look at the html that is generated for the view and see if you can tell what's happening.

I wish I could be of more help...

Tamara


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8bf2de99-6019-4f0f-998c-75bc845ece68%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Saravanan B

unread,
Jun 16, 2015, 6:51:37 AM6/16/15
to rubyonra...@googlegroups.com
Try this please it will work
<%= link_to "Destroy", {:controller => "comment", :action => "destroy", :id => comment.id }%>


Arthur Flores Duarte

unread,
Jun 16, 2015, 10:37:04 AM6/16/15
to rubyonra...@googlegroups.com
Hello everyone,

Thanks a lot for your help.
I finally found what was the problem, the jquery wasn't working. I checked the HTML code, everything was correct, but in the time of the event the jquery wasn't working,

The solution is in this post: 

In the file "\app\views\layouts\application.html.erb", inside the <head> tag I just included this line:
<%= javascript_include_tag :jquery, :jquery_ujs %>

So, now the application recognizes the Delete method, and it also solve another issue that I was having. The ajax commands using the command "remote: true" wasn't working, it was calling an html request instead of a JS request, so everything working good now. 

Thanks a lot for your help. 

Arthur Duarte

Walter Lee Davis

unread,
Jun 16, 2015, 11:13:14 AM6/16/15
to rubyonra...@googlegroups.com
Try adding those to your application.js file instead. Then they'll be concatenated and minimized by Sprockets for faster service. 

Walter
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages