我正在尝试使用以下方法删除记录:method => :删除,但它调用GET而不是。它重定向显示并显示404未找到。但如果我回去刷新,记录会被删除。
<% @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 .
我已经包含在我的布局文件中并且已经加载好了。
<%= csrf_meta_tag %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
奇怪的部分是它用于几天前工作,现在它已停止为所有控制器工作。
编辑: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
编辑: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
答案 0 :(得分:0)
我遇到了同样的问题,当我将link_to更改为button_to时,一切都开始正常运行。也许是因为link_to主要用于GET调用,而button_to用于POST调用(在我的情况下也是DELETE)。
答案 1 :(得分:-1)
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
我相信你设置正确,你只是在redirect_to调用中输了一个错字。 admins_photogallery_path是显示页面。 admins_photogalleries_path是索引页面。除非您在路由中明确指定,否则您无法进行GET请求并达到destroy方法。 GET和DELETE动词都说同一条路径 - 如果你确实在使用GET请求,你只需要进入显示页面就不会发生任何事情。任何被删除的东西都表明正在成功完成DELETE请求。