Why am I getting “No route matches [GET]” on a “delete” request? Ruby on Rails
-
03-06-2021 - |
Вопрос
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
Решение
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.
Другие советы
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.
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 = "​"
regex = /.{1,#{max_width}}/
(text.length < max_width) ? text :
text.scan(regex).join(zero_width_space)
end