2

When I try to delete the content of a communityfeed_item/micropost I get the following error:

Routing Error

No route matches [GET] "/microposts/45"
Try running rake routes for more information on available routes.

I check my routes and see a delete and post, which is all I really need to function, yet I keep receiving a "get" error.

posts GET    /posts(.:format)               posts#index
               POST   /posts(.:format)               posts#create
      new_post GET    /posts/new(.:format)           posts#new
     edit_post GET    /posts/:id/edit(.:format)      posts#edit
          post GET    /posts/:id(.:format)           posts#show
               PUT    /posts/:id(.:format)           posts#update
               DELETE /posts/:id(.:format)           posts#destroy
     locations GET    /locations(.:format)           locations#index
               POST   /locations(.:format)           locations#create
  new_location GET    /locations/new(.:format)       locations#new
 edit_location GET    /locations/:id/edit(.:format)  locations#edit
      location GET    /locations/:id(.:format)       locations#show
               PUT    /locations/:id(.:format)       locations#update
               DELETE /locations/:id(.:format)       locations#destroy
    communitys GET    /communitys(.:format)          communitys#index
               POST   /communitys(.:format)          communitys#create
 new_community GET    /communitys/new(.:format)      communitys#new
edit_community GET    /communitys/:id/edit(.:format) communitys#edit
     community GET    /communitys/:id(.:format)      communitys#show
               PUT    /communitys/:id(.:format)      communitys#update
               DELETE /communitys/:id(.:format)      communitys#destroy
         users GET    /users(.:format)               users#index
               POST   /users(.:format)               users#create
      new_user GET    /users/new(.:format)           users#new
     edit_user GET    /users/:id/edit(.:format)      users#edit
          user GET    /users/:id(.:format)           users#show
               PUT    /users/:id(.:format)           users#update
               DELETE /users/:id(.:format)           users#destroy
      sessions POST   /sessions(.:format)            sessions#create
   new_session GET    /sessions/new(.:format)        sessions#new
       session DELETE /sessions/:id(.:format)        sessions#destroy
    microposts POST   /microposts(.:format)          microposts#create
     micropost DELETE /microposts/:id(.:format)      microposts#destroy
                      /communitys(.:format)          communitys#index
                      /users(.:format)               users#index
  profile_info        /profile/info(.:format)        users#info
          join        /join(.:format)                users#new
         login        /login(.:format)               sessions#new
        logout DELETE /logout(.:format)              sessions#destroy
          root        /                              static_pages#home
          help        /help(.:format)                static_pages#help
         about        /about(.:format)               static_pages#about
       contact        /contact(.:format)             static_pages#contact
                      /posts(.:format)               static_pages#posts
       meetups        /meetups(.:format)             static_pages#meetups
          chat        /chat(.:format)                static_pages#chat
        groups        /groups(.:format)              static_pages#groups
           web        /web(.:format)                 static_pages#web
       profile        /profile(.:format)             static_pages#profile

My view code in shared/_communityfeed_item.html.erb contains a delete method:

<% if current_user?(communityfeed_item.user) %>
        <%= link_to "delete", communityfeed_item, method:  :delete,
                                         confirm: "You sure?",
                                         title:   communityfeed_item.content %>
      <% end %>

Yet, when I click delete I am sending a "get" request for some reason, which is giving a routing error. Does anyone know why this might be happening?

This is nutty, even when on my profile page where I use this delete request:

<% if current_user?(micropost.user) %>
    <%= link_to "delete", micropost, method:  :delete,
                                     confirm: "You sure?",
                                     title:   micropost.content %>
    <% end %>

I still get the no route matches [GET]!

Even weirder, this is the HTML getting rendered:

<a href=​"/​microposts/​48" title="heyheyhye ">​delete​</a>​
<a href=​"/​microposts/​47" title=​"ddsf">​delete​</a>​

etc. despite this being the embedded ruby:

<% if current_user?(communityfeed_item.user) %>
        <%= link_to "delete", communityfeed_item, method:  :delete,
                                         confirm: "You sure?",
                                         title:   communityfeed_item.content %>
      <% end %>

In the routes.rb:

Meetumea::Application.routes.draw do
  resources :comments

  resources :posts

  resources :locations

  resources :communitys
  resources :users
  resources :sessions, only: [:new, :create, :destroy]
  resources :microposts,  only: [:create, :destroy]

  match '/communitys', to: "communitys#index"

  match '/users', to: "users#index"
  match '/profile/info', to: "users#info"

  match '/join', to: "users#new"
  match '/login',  to: 'sessions#new'
  match '/logout', to: 'sessions#destroy', via: :delete

  root to: "static_pages#home"
  match '/help', to: "static_pages#help"
  match '/about', to: "static_pages#about"
  match '/contact', to: "static_pages#contact"
  match '/posts', to: "static_pages#posts"
  match '/meetups', to: "static_pages#meetups"
  match '/chat', to: "static_pages#chat"
  match '/groups', to: "static_pages#groups"
  match '/web', to: "static_pages#web"
  match '/profile', to: "static_pages#profile"
end
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Laser
  • 5,085
  • 4
  • 34
  • 48
  • I think it has something to do with the fact I have a microposts table, but no communityfeed_item table, so it is looking for the wrong thing in the database to delete perhaps? – Laser Apr 25 '12 at 19:18
  • Is your `:delete` method redirecting somewhere? – slowpoison Apr 25 '12 at 20:02
  • which version of rails do you use? try to inspect the delete button if contains attribute data-method='delete' – Suborx Apr 25 '12 at 20:24
  • rails 3.2.1 how would i inspect delete link? – Laser Apr 26 '12 at 01:20
  • inspection gives me this: ​delete​​ no delete method? what the heck? – Laser Apr 26 '12 at 01:21
  • Yet i've triple checked this is the code getting rendered: <% if current_user?(communityfeed_item.user) %> <%= link_to "delete", communityfeed_item, method: :delete, confirm: "You sure?", title: communityfeed_item.content %> <% end %> – Laser Apr 26 '12 at 01:24
  • damnit, I googled and saw this title and was like "yipee!!", except nobody has answered it... cause its my own unanswered question... – Laser Apr 26 '12 at 02:49
  • Seems something strange is going on with Rails. Have you tried restarting your webserver? Don't how far you are into development, but is it possible to create a fresh Rails app and see if it works there? – Mischa Apr 26 '12 at 02:57
  • yeh, i restart my local server (if thats what u mean by webserver = rails server) all the time. I'll try another app, except I know it works in part of the application, because I've used the the same code elsewhere... – Laser Apr 26 '12 at 03:04
  • well, not identical, but i've seen the :delete method render proper html.. – Laser Apr 26 '12 at 03:05
  • Could you show the part where it does work? And please add your routes file too, maybe there is something strange in there. – Mischa Apr 26 '12 at 03:08
  • <%= link_to 'delete', post, confirm: 'Are you sure?', method: :delete %> in index.html.erb in views/posts works fine – Laser Apr 26 '12 at 03:25
  • Hm, very strange. It shouldn't matter, but what happens if you remove `title: micropost.content` from your `link_to`? – Mischa Apr 26 '12 at 03:29
  • yeh already tried that, its weird the title, the confirm, and the delete are all not getting rendered into html. – Laser Apr 26 '12 at 03:33
  • <% if current_user?(communityfeed_item.user) %> <%= link_to 'delete', communityfeed_item, method: :delete, confirm: "You sure?", title: communityfeed_item.content %> is becoming: delete (or something like it, depending on dynamic micropost_id/content) – Laser Apr 26 '12 at 03:34
  • Well, the title *is* getting rendered. Confirm and delete are not. – Mischa Apr 26 '12 at 03:35
  • have a go: https://github.com/lasernite/meetumeb – Laser Apr 26 '12 at 03:36
  • And when you render delete link for some other model it did not work also? Try to render the post delete link. – Suborx Apr 26 '12 at 04:57
  • I thought it might be appropriate, because their actually two different questions (though one is the cause of the other, as was discovered, so I guess their very similar) – Laser Apr 26 '12 at 15:41

3 Answers3

6

Sounds like there's a problem with your unobtrusive JavaScript included in your page.

In your master layout (at layouts/application.erb.html or something thereof), do you see javascript_include_tag :application somewhere? That'll point to app/assets/javascripts/application.js. You should see this in that file.

//= require jquery
//= require jquery_ujs

The last line ensures the necessary libraries for unobtrusive JavaScript are loading with the page request.

Then in your Gemfile, you should have gem "jquery-rails". You'll want to make sure that if it's been added recently, run bundle install inside your project directory from the command line to make sure it's installed.

Do you see anything like that all? You won't see anything like rails.js loaded, I think, as that'll be with older versions of jquery-ujs or jquery-rails. When you view the source of the rendered page, you should see a data-method attribute on the delete link if the JavaScript libraries are included on the page and running properly.

I'm willing to bet — dollars to donuts — that this is a JavaScript issue. Rails uses the jquery_ujs script file to ensure "delete" links use the DELETE HTTP verb, which is similar to a POST action, but only used when deleting resources. Otherwise, the link ends up as a plain old GET action (if the JavaScript isn't loading right). jquery_ujs is responsible for emitting new link tags with things like data-method and data-confirm based on what it's given by the ERB renderer (somebody can correct me if I'm wrong about how this works; I very well could be).

Based on what your link tag is getting rendered as, I'd say something isn't right with the unobtrusive JavaScript. This describes what should be happening (specifically, the part about sending :method => symbol). At this point, I'm not sure what to tell you; something is definitely up with your JavaScript. Is it even loading? If you're using Chrome or Safari, you can use the Scripts panel in the Web Inspector to see if the Rails Asset Pipeline is pulling in the necessary scripts (jquery.js and jquery_ujs.js); if you're using Firefox, then Firebug is the way to go. Apart from that… I'm at a loss. Everything that's been stated here should work for you.

Ben Kreeger
  • 6,355
  • 2
  • 38
  • 53
  • I have: <%= javascript_include_tag "application" %>, then //= require jquery //= require jquery_ujs //= require_tree . and finally: gem 'jquery-rails', '2.0.0' – Laser Apr 26 '12 at 01:14
  • Yep, you're definitely on Rails 3.1, then. Can you post the HTML source of the delete link as it's rendered on the page? – Ben Kreeger Apr 26 '12 at 01:19
  • rails 3.2.1 Yeh I just found its wrong: ​delete​​ Yet this my code getting rendered: <% if current_user?(communityfeed_item.user) %> <%= link_to "delete", communityfeed_item, method: :delete, confirm: "You sure?", title: communityfeed_item.content %> <% end %> – Laser Apr 26 '12 at 01:26
  • What do you have in your `routes.rb` that pertains to microposts? Can you put up what you have for that? – Ben Kreeger Apr 26 '12 at 01:47
  • If you change `javascript_include_tag 'application'` to `javascript_include_tag :defaults`, does anything behave differently? – Ben Kreeger Apr 26 '12 at 02:16
  • And I noticed in the [other thread](http://stackoverflow.com/questions/5871875/no-route-matches-error-on-delete-of-micropost-in-chapter-11-of-hartls-ruby-on#comment13295327_5872607) you mentioned plugging in `rails.js` — if you're on Rails 3.2.x with a recent version of `jquery-rails`, don't do this; the necessary functionality is included in `//= require jquery_ujs`. – Ben Kreeger Apr 26 '12 at 02:22
  • By plugging in you mean adding? I added a rails.js file, so I should remove it? – Laser Apr 26 '12 at 02:26
  • Yeah, absolutely, remove the file and any traces of it (along with any traces of a custom-downloaded jQuery file). It's likely it's interfering with the built-in, newer functionality provided by `jquery_ujs`. – Ben Kreeger Apr 26 '12 at 02:27
  • That makes it so default.js tries to load, which fails because it doesn't exist. – Laser Apr 26 '12 at 02:29
  • k one sec im gonna remove the stuff and try with defaults – Laser Apr 26 '12 at 02:30
  • still getting the assets/defaults.js (not found) – Laser Apr 26 '12 at 02:31
  • So I just noticed that along with jquery.js and jquery_ujs.js loading, is a sessions.js, static_pages.js, users.js, etc. loading, however, there is no microposts.js! Could this be the issue? – Laser Apr 26 '12 at 02:34
  • thats after changing :defaults back to "application" – Laser Apr 26 '12 at 02:36
  • adding a microposts.js didn't help – Laser Apr 26 '12 at 02:38
  • Rails automatically generates those extra `.js` files for each controller you generate, but there's nothing in them unless you put code in them; they won't mess anything up by being there. – Ben Kreeger Apr 26 '12 at 02:43
  • Oh haha, yeh I think I added the micropostscontroller by hand, which would explain its absence. Any ideas? I don't know where to start, except that when I add a show to the edit, create as options, or remove restrictions in the routing, I get the "show doesn't exist". I can't understand why the html isn't rendering as it should? – Laser Apr 26 '12 at 02:47
  • If you throw your code up into a Github or Bitbucket repo, I may be able to get a better look at what's going on. – Ben Kreeger Apr 26 '12 at 02:50
  • When i go to scripts in chrome inspection I can choose between jquery_ujs.js?body=1, jquery.js?body=1 and application.js?body=1 . If loaded means the script is there, then their loaded. – Laser Apr 26 '12 at 02:52
  • under stream and profile (once logged in) are where the problems are. Another anomaly: the delete method under "posts" works fine... so how can it be javascript? unless, isolated events? – Laser Apr 26 '12 at 02:58
  • Have you tried removing the `title: such-and-such` bit from the link? Does that work? – Ben Kreeger Apr 26 '12 at 03:09
  • nope, didn't work. Good to note that neither the title nor the delete method are getting rendered in html. – Laser Apr 26 '12 at 03:11
  • I mean neither the delete method nor the confirm, the title is rendering – Laser Apr 26 '12 at 03:37
1

It's because of the call to wrap in views/static_pages/home.html.erb.

This does work:

<%= render 'shared/communityfeed' %>

This doesn't:

<%= wrap render 'shared/communityfeed' %>

It's because you are using sanitize in your wrap code:

It strips all attributes that aren't specifically allowed.

Mischa
  • 42,876
  • 8
  • 99
  • 111
  • Wow.. thats so strange, thanks a million though, I don't know if I ever would've figured that out. – Laser Apr 26 '12 at 15:40
  • It's not so strange. It's what `sanitize` does ;-) Try only sanitizing the user input not the whole partial. Good luck. – Mischa Apr 27 '12 at 04:14
0

So for some reason in the views <%= wrap render @Microposts %> and <%= wrap render 'shared/communityfeed' %> were what was causing the trouble. When I removed 'wrap' everything started working fine.

The wrap code:

def wrap(content)
  sanitize(raw(content.split.map{ |s| wrap_long_string(s) }.join(' ')))
end

def wrap_long_string(text, max_width = 50)
  zero_width_space = "&#8203;"
  regex = /.{1,#{max_width}}/
  (text.length < max_width) ? text : 
    text.scan(regex).join(zero_width_space)
end

See: https://stackoverflow.com/questions/10326180/why-is-rails-not-rendering-method-delete-into-html#comment13310733_10326180

Community
  • 1
  • 1
Laser
  • 5,085
  • 4
  • 34
  • 48