为什么我的 delete/destroy 方法和 link 不起作用?

Why does my delete/destroy method and link not work?

在我的 Ruby on Rails 应用程序中,我在 films_controller:

中有 destroy 方法
before_action :set_film, only: [:show, :edit, :update, :destroy]

def destroy
    @film = Film.find(params[:id])
    @film.destroy
    redirect_to films_path
end

从 film_params 获取参数 films_controller:

private

def film_params
    params.require(:film).permit(:title, :synopsis, :length, :director, :cast1, :cast2, :cast3, :release_date, :warnings, :certificate)
end

def set_film
  @film = Film.find(params[:id])
end

我在 index.html.erb 页面中调用此方法:

<%= link_to 'Destroy', film_path(film), method: :delete, data: { confirm: 'Are you sure?' } %>

但是每当我单击 "Destroy" 按钮时,它都会将我发送到 show.html.erb 页面,如下所示:

<div id='wrapper'>
<div id="contentWrapper">
     <div id="content">
         <div class='film'>
            <div class='large_panel film'>
                <h1><%= @film.title %></h1>
                <div class="bbfc-icons">
                    <!-- <img src="/images/bbfc/15.png" alt="15" height="40" width="40"> -->
                    <% if not @film.certificate.blank? %>
                        <%= @film.certificate.age_rating %>
                    <% end %>
                </div>
                <% if not @film.image_url.blank? %>
                    <%= image_tag @film.image_url,:size => "900x250" %>
                <% else %>
                    <%= image_tag "coming_soon.jpg",:size => "900x250" %>
                <% end %>   
                <div>
                    <div class='film_info_two_column_left'>
                        <div class='info'>
                            INFO<br>
                        </div>                    
                        <p><span class="fontBold">Starring:</span><br><% if not @film.cast1.blank? %><%= @film.cast1 %><% end %><% if not @film.cast2.blank? %>, <%= @film.cast2 %><% end %><% if not @film.cast3.blank? %>, and <%= @film.cast3 %></p><% end %>
                        <p><span class="fontBold">Director:</span> <%= @film.director %></p>
                        <p><span class="fontBold">Running Time:</span> <%= @film.length %></p>
                        <p><span class="fontBold">Certificate:</span> 15</p>          
                        <br/>
                        <p class="filmQuote whiteText"><span>SYNOPSIS:<br/></span><p>
                        <p class="filmSynopsis"><% if not @film.synopsis.blank? %><%= @film.synopsis %><% end %>                    
                    </div>
                    <div class='film_info_two_column_right time'>
                        <div class="info">
                            SCREENING TIMES
                        </div>
                        <div class="screeningTimes">
                            <% if not @film.showings.blank? %>
                                This film is shown at the following times:</p>
                                <!-- Group the showings by date, so that if there are two showings on the same date the date is only displayed once -->
                                <% @film.showings.group_by{|showing| showing.show_date.strftime("%A %e %B %Y") }.to_a.each do |showing| %>
                                    <!-- Display the times for that date in hours and minutes, joining them with a comma in between each time -->
                                    <%= showing.first %><br><%= showing.last.map{|s| s.show_time.strftime("%H:%M")}.join(', ')  %></p>            
                                <% end %>
                            <% else %>
                                <p>There are currently no showings for this film.</p>
                            <% end %>
                        </div>
                        </p><%= link_to 'All Films', films_path %> | <%= link_to 'Edit', edit_film_path(@film) %>
                    </div>
                    <div class='clearfix'></div>
                </div>
            </div>
        </div>
    </div>
</div>

还有我的routes.rb:

Rails.application.routes.draw do
    get 'films/index'

    resources :films
    resources :showings
    root 'films#index'
end

运行 rake 路线的输出:

 H:\Sites\ThorCinema\Under Construction\ThorCinema>rake routes
      Prefix Verb   URI Pattern                  Controller#Action
 films_index GET    /films/index(.:format)       films#index
       films GET    /films(.:format)             films#index
             POST   /films(.:format)             films#create
    new_film GET    /films/new(.:format)         films#new
   edit_film GET    /films/:id/edit(.:format)    films#edit
        film GET    /films/:id(.:format)         films#show
             PATCH  /films/:id(.:format)         films#update
             PUT    /films/:id(.:format)         films#update
             DELETE /films/:id(.:format)         films#destroy
    showings GET    /showings(.:format)          showings#index
             POST   /showings(.:format)          showings#create
 new_showing GET    /showings/new(.:format)      showings#new
edit_showing GET    /showings/:id/edit(.:format) showings#edit
     showing GET    /showings/:id(.:format)      showings#show
             PATCH  /showings/:id(.:format)      showings#update
             PUT    /showings/:id(.:format)      showings#update
             DELETE /showings/:id(.:format)      showings#destroy
        root GET    /                            films#index

有人可以告诉我我做错了什么吗?我完全遵循了 guidelines 但无法使删除工作。有人可以帮忙吗,这还是不行。

将 link 的方法更改为 :delete 需要 JavaScript。

  1. 确保在 application.js 中需要 jquery_ujs

    //= require jquery_ujs
    
  2. 确保 JavaScript 在您的浏览器中启用。

终于解决了,在我的 application.html.erb 文件中我遗漏了以下几行:

  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>

感谢所有试图提供帮助的人