Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-4

Rails GET method is called instead of DELETE


I am trying to delete a record using :method => :delete but its calling GET instead. It redirects to show and displays 404 not found. But if i go back and refresh, the record is acctualy deleted.

<% @photos.each do |photo| %>
<div class='photogallery'>
<%= link_to image_tag(photo.image_url(:thumb)) if photo.image? %>
<div class="name"><%= photo.caption %></div>
<div class="actions">
<%= link_to "edit", edit_admins_photogallery_path(photo) %> |
<%= link_to "remove",admins_photogallery_path(photo), :method => 'delete',:confirm => 'Are you sure?' %> |

</div>
</div>
<% end %>

application.js

//= require jquery
//= require jquery_ujs
//= require jquery-fileupload/basic
//= require jquery-fileupload/vendor/tmpl
//= require dataTables/jquery.dataTables
//= require bootstrap
//= require bootstrap-select
//= require jquery.timepicker
//= require jquery.ui.tabs
//= require jquery.ui.datepicker
//= require jquery.ui.accordion
//= require jquery.ui.autocomplete
//= require strftime-min.js
//= require turbolinks
//= require_tree .

i have included in my layout file and its been loaded fine.

<%= csrf_meta_tag %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>

The weird part is it used to work fine few days back,now it has stopped working for all the controllers.

edit: routes.rb

devise_for :users
root 'index#index'
resources :reprint
devise_scope :user do
get "/admins/", :to => "devise/sessions#new"
end
namespace :admins do
resources :location,:dashboard,:lodge,:room,:booking,:rate_calendar,:photogallery    
end  
resources :index do 
collection do
match 'search' => 'index#search', via: [:get, :post], as: :search
match 'location' => 'index#location', via: [:get, :post], as: :location
match  'add_cart' => 'index#add_cart', via: [:get, :post], as: :add_cart
end
end
resources :room_availability

edit: photogallery_controller.rb

def destroy
  @photos = Photogallery.find(params[:id])
 if @photos.destroy
  redirect_to admins_photogallery_path, notice: "Photo was successfully destroyed."
 else
  render :action => 'index'
  flash[:error] = "Photo could not be deleted."
 end
end

Solution

  • def destroy
      @photos = Photogallery.find(params[:id])
     if @photos.destroy
      redirect_to admins_photogalleries_path, notice: "Photo was successfully destroyed."
     else
      render :action => 'index'
      flash[:error] = "Photo could not be deleted."
     end
    end
    

    I believe you have it set up correctly, you just made a typo in the redirect_to call. admins_photogallery_path is the show page. admins_photogalleries_path is the index page. There's no way you're making a GET request and reaching the destroy method unless you specified that specifically in the routes. The GET and DELETE verbs both talk to the same path - if you were indeed using a GET request, you'd simply go to the show page and nothing would happen. That anything got deleted at all suggests that the DELETE request is being made successfully.