为什么我的删除/销毁方法和链接不起作用?

时间:2015-01-23 10:02:28

标签: ruby-on-rails ruby destroy

在我的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

这是从films_controller中的film_params中获取参数:

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?' } %>

但每当我点击“销毁”按钮时,它会将我发送到 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但无法使删除工作。有人可以帮忙,这仍然无法正常工作。

2 个答案:

答案 0 :(得分:2)

将链接的方法更改为:delete需要使用JavaScript。

  1. 确保jquery_ujs中需要application.js

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

答案 1 :(得分:0)

最后解决了它,在我的application.html.erb文件中我错过了以下几行:

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

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