Search code examples
ruby-on-railsrubydestroy

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


In my Ruby on Rails application I have the destroy method in films_controller:

before_action :set_film, only: [:show, :edit, :update, :destroy]

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

Which is getting the params from film_params in 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

And I am calling this method in my index.html.erb page:

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

But whenever I click the "Destroy" button it sends me to the show.html.erb page, which is as follows:

<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>

And my routes.rb:

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

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

Output from running rake routes:

 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

Can someone please show me what I am doing wrong? I have followed the guidelines exactly but cannot get the delete to work. Can someone please help, this still isn't working.


Solution

  • Finally solved it, in my application.html.erb file I was missing the following lines:

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

    Thank you to all who tried to help