0

i am new in RoR and have a problem with Ruby on Rails because this doesn't delete records when i clicked the button Destroy. I create a new project and using the command scaffold to create it.

My view:

<h1>CARTELERA</h1>

<table>
  <thead>
    <tr>
      <th>Titulo</th>
      <th>Genero</th>
      <th>Director</th>
      <th>Duracion</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @peliculas.each do |pelicula| %>
      <tr>
        <td><%= pelicula.titulo %></td>
        <td><%= pelicula.genero %></td>
        <td><%= pelicula.director %></td>
        <td><%= pelicula.duracion %></td>
        <td><%= link_to 'Mostrar', pelicula %></td>
        <td><%= link_to 'Editar', edit_pelicula_path(pelicula) %></td>
        <td><%= link_to 'Eliminar', pelicula, method: :delete, data: { confirm: 'Estás seguro?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'Nueva Película', new_pelicula_path %>

My method desploy in controller:

def destroy
    @pelicula.destroy
    respond_to do |format|
      format.html { redirect_to peliculas_url, notice: 'Pelicula was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

Output Rake routes

C:\Cine>rake routes
       Prefix Verb   URI Pattern                   Controller#Action

    peliculas GET    /peliculas(.:format)          peliculas#index
              POST   /peliculas(.:format)          peliculas#create
 new_pelicula GET    /peliculas/new(.:format)      peliculas#new
edit_pelicula GET    /peliculas/:id/edit(.:format) peliculas#edit
     pelicula GET    /peliculas/:id(.:format)      peliculas#show
              PATCH  /peliculas/:id(.:format)      peliculas#update
              PUT    /peliculas/:id(.:format)      peliculas#update
              DELETE /peliculas/:id(.:format)      peliculas#destroy
         root GET    /                             peliculas#index

My controller comlete

class PeliculasController < ApplicationController
  before_action :set_pelicula, only: [:show, :edit, :update, :destroy]

  # GET /peliculas
  # GET /peliculas.json
  def index
    @peliculas = Pelicula.all
  end

  # GET /peliculas/1
  # GET /peliculas/1.json
  def show
  end

  # GET /peliculas/new
  def new
    @pelicula = Pelicula.new
  end

  # GET /peliculas/1/edit
  def edit
  end

  # POST /peliculas
  # POST /peliculas.json
  def create
    @pelicula = Pelicula.new(pelicula_params)

    respond_to do |format|
      if @pelicula.save
        format.html { redirect_to @pelicula, notice: 'Pelicula was successfully created.' }
        format.json { render :show, status: :created, location: @pelicula }
      else
        format.html { render :new }
        format.json { render json: @pelicula.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /peliculas/1
  # PATCH/PUT /peliculas/1.json
  def update
    respond_to do |format|
      if @pelicula.update(pelicula_params)
        format.html { redirect_to @pelicula, notice: 'Pelicula was successfully updated.' }
        format.json { render :show, status: :ok, location: @pelicula }
      else
        format.html { render :edit }
        format.json { render json: @pelicula.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /peliculas/1
  # DELETE /peliculas/1.json
  def destroy
    @pelicula.destroy
    respond_to do |format|
      format.html { redirect_to peliculas_url, notice: 'Pelicula was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_pelicula
      @pelicula = Pelicula.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def pelicula_params
      params.require(:pelicula).permit(:titulo, :genero, :director, :duracion)
    end
end
potashin
  • 44,205
  • 11
  • 83
  • 107

1 Answers1

0

I think it's because you have the syntax wrong. try adding this :method => :delete right now you have <%= link_to 'Eliminar', pelicula, method: :delete, data: { confirm: 'Estás seguro?' } %>. Below is a way to debug if request are being sent or not.

When your running your server locally check the logs in the terminal window.

Pressing enter a few times within the terminal, while the server is running will give you some negative space inbetween the last action a new action your app will preform.

enter image description here

After you've added some negitave space in the terminal, Go back to the app and press delete on the item.

Go back to your terminal and find the gap of space If you see

enter image description here

Started DELETE "/peliculas/{Some Number}" Processing by PeliculasController#destroy as HTML That means it's working.

I'm pretty sure it could be because your syntax after the 'Eliminar' is wrong change it to, :method => :delete instead of method: :delete