2015-06-30 139 views
0

我是RoR中的新成员,并且Ruby on Rails存在问题,因为当我单击按钮Destroy时不会删除记录。 我创建一个新项目并使用命令脚手架来创建它。Ruby on Rails不会使用脚手架删除

我的观点:在控制器

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

我的方法desploy:

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 

我控制器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 
+0

编辑的问题... –

+0

什么你点击删除按钮后会发生什么? – forthowin

+0

试试这里的答案http://stackoverflow.com/questions/12520456/execjsruntimeerror-on-windows-trying-to-follow-rubytutorial – forthowin

回答

0

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

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

在终端内按下输入几次,而服务器运行时会给你一些负面的空间,在上一次操作中你的应用会执行一个新动作。

enter image description here

您添加的终端一些negitave空间之后,返回到应用程序,并按下项删除。

回到你的终端,并找到空间的差距。如果你看到

enter image description here

开始删除“/ peliculas/{某些数}” 处理由PeliculasController#摧毁HTML 这意味着它的加工。

我敢肯定,这可能是因为“Eliminar”后你的语法是错误的改变它,而不是:method => :deletemethod: :delete