Ruby on Rails不会使用scaffolding删除

时间:2015-06-30 01:07:36

标签: ruby-on-rails

我是RoR的新手,并且在Ruby on Rails上遇到问题,因为当我点击按钮Destroy时,这并没有删除记录。 我创建了一个新项目并使用命令scaffold来创建它。

我的观点:

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

我的方法在控制器中取消部署:

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

输出佣金路线

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

我的控制器完成

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

1 个答案:

答案 0 :(得分:0)

我认为这是因为你的语法错了。现在尝试添加此:method => :delete <%= link_to 'Eliminar', pelicula, method: :delete, data: { confirm: 'Estás seguro?' } %>。以下是调试请求是否正在发送的方法。

当您在本地运行服务器时,请在终端窗口中检查日志。

在终端内输入几次,而服务器正在运行时会在最后一次操作之间给你一些负面空间,你的应用程序将执行新操作。

enter image description here

在终端中添加了一些negitave空间后,返回应用程序并按项目上的删除。

回到终端找到空间的间隙如果你看到了

enter image description here

已开始删除“/ peliculas / {Some Number}” 由PeliculasController处理#销毁为HTML 这意味着它正在发挥作用。

我很确定这可能是因为'Eliminar'之后的语法错误更改为:method => :delete而不是method: :delete