3

I am trying to track down a particularly elusive bug in working through Michael Hartl's ROR Tutorial.

When clicking on 'Delete' for a micropost (from the home page or the user/show page) the url is http://localhost:3000/microposts/303, but the result is "Routing Error - No route matches"/microposts/303".

I have gone through each page of my code that is involved and replaced them with code from Hartl's gitHub project site. https://github.com/railstutorial/sample_app. For example, for the microposts_controller, I copied the code from the gitHub depot and replaced my code with the copied code. I then restarted the server. Same result. I then reverted back to my code to test the next page.

The pages I swapped the code with are

CONTROLLERS microposts_controller users_controller (show method)

MODEL micropost.rb (model)

VIEWS microposts/_micropost.haml shared/_micropost_form.html.haml shared/_feed.haml shared/_feed_item.haml

and the Routes file.

I am at a loss for other things to check. Does anyone have any suggestions?

Thanks,

Dave

The results of rake routes

 sessions POST   /sessions(.:format)       {:action=>"create", :controller=>"sessions"}
new_session GET    /sessions/new(.:format)   {:action=>"new", :controller=>"sessions"}
    session DELETE /sessions/:id(.:format)   {:action=>"destroy", :controller=>"sessions"}
     signin        /signin(.:format)         {:controller=>"sessions", :action=>"new"}
    signout        /signout(.:format)        {:controller=>"sessions", :action=>"destroy"}
 microposts POST   /microposts(.:format)     {:action=>"create", :controller=>"microposts"}
  micropost DELETE /microposts/:id(.:format) {:action=>"destroy", :controller=>"microposts"}
       root        /(.:format)               {:controller=>"pages", :action=>"home"}
    contact        /contact(.:format)        {:controller=>"pages", :action=>"contact"}
      about        /about(.:format)          {:controller=>"pages", :action=>"about"}
       help        /help(.:format)           {:controller=>"pages", :action=>"help"}
     signup        /signup(.:format)         {:controller=>"users", :action=>"new"}
development        /development(.:format)    {:controller=>"pages", :action=>"development"}
                   /signup(.:format)         {:controller=>"users", :action=>"new"}
      users GET    /users(.:format)          {:action=>"index", :controller=>"users"}
            POST   /users(.:format)          {:action=>"create", :controller=>"users"}
   new_user GET    /users/new(.:format)      {:action=>"new", :controller=>"users"}
  edit_user GET    /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
       user GET    /users/:id(.:format)      {:action=>"show", :controller=>"users"}
            PUT    /users/:id(.:format)      {:action=>"update", :controller=>"users"}
            DELETE /users/:id(.:format)      {:action=>"destroy", :controller=>"users"}

The Routes.rb file is

SampleApp::Application.routes.draw do

#Sign in Routes
  resources :sessions, :only => [:new, :create, :destroy]
  match '/signin', :to => 'sessions#new'
  match '/signout', :to => 'sessions#destroy'

#Microposts Routes
  resources :microposts, :only => [:create, :destroy]


#Pages Routes
  root :to => "pages#home"

  match '/contact', :to => 'pages#contact'
  match '/about',   :to => 'pages#about'
  match '/help',    :to => 'pages#help'
  match '/signup',  :to => 'users#new'
  match '/development', :to => 'pages#development'

#Users Routes
  match '/signup',  :to => 'users#new'
  resources :users

end

But, as I said, even replacing my routes file with the one on gitHub did not resolve the issue.

The link to delete is

 = link_to "delete", micropost, :method => :delete,
                                      :confirm => "You sure?",
                                      :title => micropost.content
humbledaisy
  • 373
  • 1
  • 3
  • 12

1 Answers1

3

link_to :method => :delete uses unobtrusive javascript to create the DELETE request. My guess is that you either don't have the necessary javascript files in your project (prototype.js/jquery.js and rails.js) or you are not including them in your layout.

Austin Taylor
  • 5,437
  • 1
  • 23
  • 29
  • Thanks @Austin, unfortunately, that was not it. Both prototype.js and rails.js are loading. – humbledaisy May 03 '11 at 16:56
  • Ooohhh @Austin, you are definitely onto something though. I was also loading jquery. I tested commenting jquery out, and it worked. Switching the order worked also. Any idea why? – humbledaisy May 03 '11 at 18:15
  • 1
    @humbledaisy if you are trying to use both jQuery and Prototype in the same project, you are going to have to put jQuery in compatibility mode because they conflict on the `$` function. See here: http://docs.jquery.com/Using_jQuery_with_Other_Libraries – Austin Taylor May 03 '11 at 18:17
  • I also neglected to Thank you @Austin. Thank you very much!! – humbledaisy May 03 '11 at 18:18
  • @humbledaisy If you just want to use jQuery with Rails, you can get the jQuery version of rails.js (https://github.com/rails/jquery-ujs) and remove Prototype completely. – Austin Taylor May 03 '11 at 18:18
  • One more note; In looking into jquery_ujs I came across a gem that handles removing prototype, downloading and installing the jquery you want and the ujs bridge. It's available at https://github.com/indirect/jquery-rails – humbledaisy May 03 '11 at 18:29
  • I have jquery.js and jquery_ujs.js showing as loaded in the source, but no rails.js. Where/how do i get/add rails.js if it's neccesary? Does having two jquery's a problem, should I remove jquery.js and leave only the jquery_ujs? I'm having the same problem but have no protype, so I figure it must be the lack of rails.js/ having two jquery. Thanks. – Laser Apr 26 '12 at 00:56
  • So I put the code from https://raw.github.com/rails/jquery-ujs/master/src/rails.js into app/assets/javascripts/rails.js and the code from http://code.jquery.com/jquery-1.7.2.js into app/assets/javascripts/jquery.js It has done nothing. Still the error: Routing Error No route matches [GET] "/microposts/48" Try running rake routes for more information on available routes. – Laser Apr 26 '12 at 01:08